底部圆角的自定义视图在 Swift 中没有阴影效果
Customview with bottom round corners not getting shadow effect in Swift
我正在展示一些自定义视图。我试图只显示带有底部阴影的底角。它能够得到底部圆角,但没有阴影效果。如果我去掉角,阴影效果就来了。
func showCustomView() {
self.customview.layer.shadowOffset = CGSize(width: 0, height: 5)
self.customview.layer.shadowOpacity = 0.6
self.customview.layer.shadowRadius = 3.0
self.customview.layer.shadowColor = UIColor.gray.cgColor
self.customview.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 12)
}
extension UIView {
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
if #available(iOS 11, *) {
self.clipsToBounds = true
self.layer.cornerRadius = radius
var masked = CACornerMask()
if corners.contains(.topLeft) { masked.insert(.layerMinXMinYCorner) }
if corners.contains(.topRight) { masked.insert(.layerMaxXMinYCorner) }
if corners.contains(.bottomLeft) { masked.insert(.layerMinXMaxYCorner) }
if corners.contains(.bottomRight) { masked.insert(.layerMaxXMaxYCorner) }
self.layer.maskedCorners = masked
}
else {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}
}
有什么建议吗?
删除self.clipsToBounds = true
来自 roundCorners(...)
例子
import UIKit
class MissingShadowViewController: UIViewController {
@IBOutlet weak var customView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
showCustomView()
}
func showCustomView() {
self.customView.layer.shadowOffset = CGSize(width: 0, height: 5)
self.customView.layer.shadowOpacity = 0.6
self.customView.layer.shadowRadius = 3.0
self.customView.layer.shadowColor = UIColor.gray.cgColor
self.customView.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 12)
}
}
extension UIView {
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
if #available(iOS 11, *) {
// clipping here will hide shadows
// self.clipsToBounds = true
self.layer.cornerRadius = radius
var masked = CACornerMask()
if corners.contains(.topLeft) { masked.insert(.layerMinXMinYCorner) }
if corners.contains(.topRight) { masked.insert(.layerMaxXMinYCorner) }
if corners.contains(.bottomLeft) { masked.insert(.layerMinXMaxYCorner) }
if corners.contains(.bottomRight) { masked.insert(.layerMaxXMaxYCorner) }
self.layer.maskedCorners = masked
}
else {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}
}
我正在展示一些自定义视图。我试图只显示带有底部阴影的底角。它能够得到底部圆角,但没有阴影效果。如果我去掉角,阴影效果就来了。
func showCustomView() {
self.customview.layer.shadowOffset = CGSize(width: 0, height: 5)
self.customview.layer.shadowOpacity = 0.6
self.customview.layer.shadowRadius = 3.0
self.customview.layer.shadowColor = UIColor.gray.cgColor
self.customview.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 12)
}
extension UIView {
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
if #available(iOS 11, *) {
self.clipsToBounds = true
self.layer.cornerRadius = radius
var masked = CACornerMask()
if corners.contains(.topLeft) { masked.insert(.layerMinXMinYCorner) }
if corners.contains(.topRight) { masked.insert(.layerMaxXMinYCorner) }
if corners.contains(.bottomLeft) { masked.insert(.layerMinXMaxYCorner) }
if corners.contains(.bottomRight) { masked.insert(.layerMaxXMaxYCorner) }
self.layer.maskedCorners = masked
}
else {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}
}
有什么建议吗?
删除self.clipsToBounds = true
来自 roundCorners(...)
例子
import UIKit
class MissingShadowViewController: UIViewController {
@IBOutlet weak var customView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
showCustomView()
}
func showCustomView() {
self.customView.layer.shadowOffset = CGSize(width: 0, height: 5)
self.customView.layer.shadowOpacity = 0.6
self.customView.layer.shadowRadius = 3.0
self.customView.layer.shadowColor = UIColor.gray.cgColor
self.customView.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 12)
}
}
extension UIView {
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
if #available(iOS 11, *) {
// clipping here will hide shadows
// self.clipsToBounds = true
self.layer.cornerRadius = radius
var masked = CACornerMask()
if corners.contains(.topLeft) { masked.insert(.layerMinXMinYCorner) }
if corners.contains(.topRight) { masked.insert(.layerMaxXMinYCorner) }
if corners.contains(.bottomLeft) { masked.insert(.layerMinXMaxYCorner) }
if corners.contains(.bottomRight) { masked.insert(.layerMaxXMaxYCorner) }
self.layer.maskedCorners = masked
}
else {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}
}