我为此做了一个自定义类。但这仅适用于左上角,不适用于其他:
@IBDesignable
public class RoundedView: UIView {
@IBInspectable public var topLeft: Bool = false
@IBInspectable public var topRight: Bool = false
@IBInspectable public var bottomLeft: Bool = false
@IBInspectable public var bottomRight: Bool = false
@IBInspectable public var cornerRadius: Int = 0
public override func awakeFromNib() {
var options = UIRectCorner()
if topLeft { options = options.union(.topLeft) }
if topRight { options = options.union(.topRight) }
if bottomLeft { options = options.union(.bottomLeft) }
if bottomRight { options = options.union(.bottomRight) }
let path = UIBezierPath(roundedRect:self.bounds,
byRoundingCorners:options,
cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
self.layer.mask = maskLayer
}
}
这是在模拟器中运行时的外观,我将所有四个角都应用了角半径:
这是怎么回事? TopLeft起作用,TopRight稍微起作用,而下角完全不起作用。我很困惑。
答案 0 :(得分:5)
您应该在layoutSubviews
的替代值中更新掩码。当约束条件更新视图的frame
时,将调用layoutSubviews
,因此这是更新蒙版的正确位置。
顺便说一句,我不鼓励使用awakeFromNib
来配置视图。如果您使用情节提要,效果很好,但是以编程方式创建的视图将不会调用此功能。如果确实要在创建视图时对其进行配置,则最好将代码放入由init(frame:)
和init(coder:)
调用的某种私有配置方法中。这样,它既适用于情节提要和以编程方式创建的视图。
我也可能建议观察者注意可检查的属性,以触发对角倒圆的更新。如果要更改这些属性,则希望圆角自动更新。
因此:
@IBDesignable
public class RoundedView: UIView {
@IBInspectable public var topLeft: Bool = false { didSet { setNeedsLayout() } }
@IBInspectable public var topRight: Bool = false { didSet { setNeedsLayout() } }
@IBInspectable public var bottomLeft: Bool = false { didSet { setNeedsLayout() } }
@IBInspectable public var bottomRight: Bool = false { didSet { setNeedsLayout() } }
@IBInspectable public var cornerRadius: CGFloat = 0 { didSet { setNeedsLayout() } }
public override func layoutSubviews() {
super.layoutSubviews()
var options = UIRectCorner()
if topLeft { options.formUnion(.topLeft) }
if topRight { options.formUnion(.topRight) }
if bottomLeft { options.formUnion(.bottomLeft) }
if bottomRight { options.formUnion(.bottomRight) }
let path = UIBezierPath(roundedRect: bounds,
byRoundingCorners: options,
cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
layer.mask = maskLayer
}
}
或者,或者,如果定位到iOS 11及更高版本,我们可以让CoreAnimation进行四舍五入:
@IBDesignable
public class RoundedView: UIView {
@IBInspectable public var topLeft: Bool = false { didSet { updateCorners() } }
@IBInspectable public var topRight: Bool = false { didSet { updateCorners() } }
@IBInspectable public var bottomLeft: Bool = false { didSet { updateCorners() } }
@IBInspectable public var bottomRight: Bool = false { didSet { updateCorners() } }
@IBInspectable public var cornerRadius: CGFloat = 0 { didSet { updateCorners() } }
public override init(frame: CGRect = .zero) {
super.init(frame: frame)
updateCorners()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
updateCorners()
}
}
private extension RoundedView {
func updateCorners() {
var corners = CACornerMask()
if topLeft { corners.formUnion(.layerMinXMinYCorner) }
if topRight { corners.formUnion(.layerMaxXMinYCorner) }
if bottomLeft { corners.formUnion(.layerMinXMaxYCorner) }
if bottomRight { corners.formUnion(.layerMaxXMaxYCorner) }
layer.maskedCorners = corners
layer.cornerRadius = cornerRadius
}
}
关于后一种方法的好处是,如果我们恰巧为视图的frame
设置动画,则CoreAnimation将兑现我们的圆角:
如果使用上述layoutSubviews
方法,则必须手动管理动画(例如,使用CADisplayLink
)。