ios 如何为UIBezierPath给予cornerRadius

acruukt9  于 2023-06-07  发布在  iOS
关注(0)|答案(6)|浏览(174)

我用下面的代码创建了一个矩形,现在我需要圆这个矩形的角。但是我找不到一个叫做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
    }
}
klsxnrf1

klsxnrf11#

您可以使用下面的方法来使所有的角落圆的看法...

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

eqfvzcg82#

这样使用:

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

j0pj023g3#

使用此初始化器

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

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

r8xiu3jd4#

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

希望这会有所帮助。

wbrvyc0a

wbrvyc0a5#

就像这样简单:

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

nfeuvbwi6#

如果要使矩形的角变圆,可以使用

UIBezierPath(roundedRect:  CGRect, byRoundingCorners: UIRectCorner,
cornerRadii: CGSize)

但是如果你想把它们做成椭圆形,你就做不到。这是因为UIBezierPath忽略了cornerRadiii.height。我在我的实现中修复了它。
https://github.com/BuTaJIuK-ua/UIBezierPath-Extension

相关问题