简体   繁体   中英

UIBezierPath and CAShapeLayer not creating corner line

This is my code

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])

Here my output

在此处输入图片说明

Can anybody explain where I'm wrong with ? It's on Swift 3.

Thanks,

Hey I got the answer!!!

Try with UIBezierPath and CAShapeLayer not creating corner line

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)

Better To Use a generic Extension:

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

}
}

Call to the Function:

 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

Note: Either declare the extention in the ViewController file outside the class or in a sperate swift file

产量

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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