如何在Swift 4中初始化由代码生成的按钮边框

时间:2019-02-26 08:54:58

标签: ios swift uibutton border calayer

我用代码创建了按钮边框。

由于某种原因,我必须更改按钮边框的形状。

但是,遮罩生成的边框未使用以下代码初始化。

button.backgroundColor = .clear
button.layer.CornerRadius = 0

在ViewController.swift中:

@IBOutlet weak var btnDelete: UIButton!

func FirstChange() {
    btnDelete.layer.borderWidth = 0
    btnDelete.layer.cornerRadius = 0
    btnDelete.layer.borderColor = UIColor(rgb: 0xFFFFFF).cgColor
    // Draw the border again
    btnDelete.round(corners: [.topRight, .bottomRight], radius: 50, borderColor: UIColor(rgb: 0xced4da), borderWidth: 1)
}

func SecChange() {
    btnDelete.backgroundColor = .clear // not work
    // Draw the border again
    btnDelete.layer.borderColor = UIColor(rgb: 0xced4da).cgColor
    btnDelete.layer.borderWidth = 1
    btnDelete.layer.cornerRadius = 18
}

在UIView.swift中:

func round(corners: UIRectCorner, radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
    let mask = _round(corners: corners, radius: radius)
    addBorder(mask: mask, borderColor: borderColor, borderWidth: borderWidth)
}

@discardableResult func _round(corners: UIRectCorner, radius: CGFloat) -> CAShapeLayer {
    let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
    let mask = CAShapeLayer()
    mask.path = path.cgPath
    self.layer.mask = mask
    return mask
}

func addBorder(mask: CAShapeLayer, borderColor: UIColor, borderWidth: CGFloat) {
    let borderLayer = CAShapeLayer()
    borderLayer.path = mask.path
    borderLayer.fillColor = UIColor.clear.cgColor
    borderLayer.strokeColor = borderColor.cgColor
    borderLayer.lineWidth = borderWidth
    borderLayer.frame = bounds
    layer.addSublayer(borderLayer)
}

第二次绘制边框(运行SecChange()),它与第一个边框重叠。

请帮助我初始化我绘制的第一个边框。

(运行SecChange()和运行FirstChange()成功地初始化了边界。)

1 个答案:

答案 0 :(得分:0)

由于您要向CAShapeLayer添加UIButton,因此需要从按钮中删除该层。为此,您可以给图层一个name并添加一个新方法以删除该图层并在第二次更改中调用该新方法。另外,在再次调用round(corners:radius:borderColor:borderWidth:)时,应删除边框层,否则最终将在另一层上。

func SecChange() {
        btnDelete.removeBorderLayer() //remove border layer if existing
        // Draw the border again
        btnDelete.layer.borderColor = UIColor.gray.cgColor
        btnDelete.layer.borderWidth = 1
        btnDelete.layer.cornerRadius = 18
    }
extension UIView {
    func round(corners: UIRectCorner, radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
        let mask = _round(corners: corners, radius: radius)
        addBorder(mask: mask, borderColor: borderColor, borderWidth: borderWidth)
    }

    @discardableResult func _round(corners: UIRectCorner, radius: CGFloat) -> CAShapeLayer {
        let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
        return mask
    }

    func addBorder(mask: CAShapeLayer, borderColor: UIColor, borderWidth: CGFloat) {
        removeBorderLayer()
        let borderLayer = CAShapeLayer()
        borderLayer.name = "borderLayer"
        borderLayer.path = mask.path
        borderLayer.fillColor = UIColor.clear.cgColor
        borderLayer.strokeColor = borderColor.cgColor
        borderLayer.lineWidth = borderWidth
        borderLayer.frame = bounds
        layer.addSublayer(borderLayer)
    }

    func removeBorderLayer() {
        if let borderLayer = layer.sublayers?.first(where: { $0.name == "borderLayer" }) {
            borderLayer.removeFromSuperlayer()
        }
    }
}

最好, 卡斯滕