如何为UIBezierPath提供cornerRadius

时间:2016-01-28 05:54:55

标签: ios swift uibezierpath cashapelayer cornerradius

我使用以下代码创建了一个矩形,现在我需要对此矩形的角进行舍入。但我找不到名为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
    }
}

5 个答案:

答案 0 :(得分:9)

您可以使用以下方法制作所有角落视图...

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

答案 1 :(得分:6)

像这样使用:

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

for All Corner Radius编辑此

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

答案 2 :(得分:2)

目标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)

希望这会有所帮助。

答案 3 :(得分:2)

使用此初始化程序

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

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

答案 4 :(得分:0)

很简单:

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