如何为 UIBezierPath 提供 cornerRadius

How to give cornerRadius for UIBezierPath

我使用以下代码创建了一个矩形,现在我需要圆化这个矩形的角。但我找不到名为 layer.cornerRadius 的 属性,有人可以帮我吗?

class OvalLayer: CAShapeLayer {

    let animationDuration: CFTimeInterval = 0.3

    override init() {
        super.init()
        fillColor = Colors.green.CGColor
        path = ovalPathSmall.CGPath
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    var ovalPathStart: UIBezierPath {
        let path = UIBezierPath(ovalInRect: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))

        return path
    }
}

Objective c:

UIBezierPath* path = [UIBezierPath
    bezierPathWithRoundedRect: CGRectMake(0, 0, 150, 153)
                 cornerRadius: 50];

SWIFT :

var path: UIBezierPath = UIBezierPath(roundedRect: CGRectMake(0, 0, 150, 153), cornerRadius: 50)

希望这会有所帮助。

使用这个初始化器

let path1 = UIBezierPath(roundedRect: CGRect, cornerRadius: CGFloat)

let path1 = UIBezierPath(roundedRect:  CGRect, byRoundingCorners: UIRectCorner, cornerRadii: CGSize))

您可以使用下面的方法来制作所有角落的圆形视图...

 UIBezierPath(roundedRect: anyView.bounds, cornerRadius: CGSize(width: 10.0, height: 10.0))

如果你想让特定的角变圆,使用下面的方法。

 UIBezierPath(roundedRect: anyView.bounds,
        byRoundingCorners: .BottomLeft | .BottomRight,
        cornerRadius: CGSize(width: 10.0, height: 10.0))

这样使用:

let pathWithRadius = UIBezierPath(roundedRect:yourView.bounds, byRoundingCorners:[.TopRight, .TopLeft], cornerRadii: CGSizeMake(5.0, 5.0))
let maskLayer = CAShapeLayer()
maskLayer.pathWithRadius = pathWithRadius.CGPath
yourView.layer.mask = maskLayer

对于所有角半径编辑此

[.TopRight,.TopLeft,.BottomRight, .BottomLeft]

简单如:

 UIBezierPath(roundedRect: anyView.bounds, cornerRadius: CGFloat(4.0))