【发布时间】:2019-05-14 11:07:44
【问题描述】:
【问题讨论】:
-
只需将半径应用于主容器视图(不需要按钮)
-
@SPatel 我已经为整个视图设置了圆角半径,但按钮没有改变
【问题讨论】:
只需创建如下所示的自定义视图,并将蒙版和路径应用于主层
@IBDesignable class VariableSeparateCornerView: UIView {
private var corners: UIRectCorner = [.allCorners]
private var radius: CGFloat = 0
override func layoutSubviews() {
super.layoutSubviews()
self.refreshCorners()
}
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
self.corners = corners
self.radius = radius
self.refreshCorners()
}
private func refreshCorners() {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
}
用途:
let containerView = VariableSeparateCornerView()
containerView.roundCorners(corners: [.topRight, .bottomLeft, .bottomRight], radius: 20)
【讨论】:
这是扩展版本。这可能更容易。
extension UIView {
func roundCorners(_ corners:UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
}
用途:
button?.roundCorners([.topRight, .bottomLeft, .bottomRight], radius: radius)
【讨论】:
你可以这样试试。
if #available(iOS 11.0, *) {
customView.layer.cornerRadius = 10
customView.layer.maskedCorners = [.layerMinXMaxYCorner,.layerMinXMinYCorner,.layerMaxXMinYCorner]
} else {
// Fallback on earlier versions
let rectShape = CAShapeLayer()
rectShape.bounds = customView.frame
rectShape.position = customView.center
rectShape.path = UIBezierPath(roundedRect: customView.bounds, byRoundingCorners: [.bottomRight , .topLeft , .topRight], cornerRadii: CGSize(width: 10, height: 10)).cgPath
customView.layer.mask = rectShape
}
【讨论】: