繁体   English   中英

UIBezierPath和CAShapeLayer未创建角线

[英]UIBezierPath and CAShapeLayer not creating corner line

这是我的代码

public func setMaskTo(setMask:UIView, corner:UIRectCorner) -> Void {
        let round = UIBezierPath(roundedRect: setMask.bounds, byRoundingCorners: corner, cornerRadii: CGSize(width: 10, height: 10))
        let shape = CAShapeLayer()
        shape.frame = round.bounds
        shape.path = round.cgPath
        setMask.layer.mask = shape
    }

setMaskTo(setMask: self, corner: [.topLeft, .topRight])

这是我的输出

在此处输入图片说明

谁能解释我错在哪里? 在Swift 3上。

谢谢,

嘿,我得到了答案!!!

尝试使用UIBezierPath和CAShapeLayer不创建角线

var borderShape = CAShapeLayer() 
borderShape.path = round.cgPath
borderShape.lineWidth = 1.0
borderShape.strokeColor = UIColor.blue.cgColor
borderShape.fillColor = nil
borderShape.frame = setMask.bounds
setMask.layer.addSublayer(borderShape)

更好地使用通用扩展名:

extension UIView {

func roundedCorners(corners: UIRectCorner, conrnerRadius: CGFloat) {

    let rectShape = CAShapeLayer()
    rectShape.bounds = self.frame

    rectShape.position = self.center
    rectShape.path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: [UIRectCorner.TopLeft , UIRectCorner.TopRight], cornerRadii: CGSizeMake(conrnerRadius, conrnerRadius)).CGPath

    self.layer.mask = rectShape

}
}

调用函数:

 oneextraview.roundedCorners(corners: [UIRectCorner.topRight, UIRectCorner.bottomRight], conrnerRadius: 10.0)
 oneextraview.layer.borderColor = UIColor.redColor().CGColor
 oneextraview.layer.borderWidth = 5.0
 oneextraview.layer.masksToBounds = true

注意:在类外部的ViewController文件中或在单独的swift文件中声明扩展

产量

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM