不适用于Storyboard中特定UIView角的角半径不适用于所有角

Adr*_*ian 3 ios uistoryboard swift

我为此做了一个自定义类。但这仅适用于左上角,不适用于其他:

@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
    }
}
Run Code Online (Sandbox Code Playgroud)

这是在模拟器中运行时的外观,我将所有4个角都应用了角半径:

在此处输入图片说明

这里发生了什么 ?TopLeft起作用,TopRight稍微起作用,而下角完全不起作用。我很困惑。

Rob*_*Rob 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
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,或者,如果定位到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
    }
}
Run Code Online (Sandbox Code Playgroud)

关于后一种方法的好处是,如果我们碰巧frame将视图的动画化,则CoreAnimation将尊重我们的圆角处理:

在此处输入图片说明

如果您使用上述layoutSubviews方法,则必须手动管理动画(例如使用CADisplayLink)。