2016-01-28 20 views
4

我使用下面的代码创建了一个矩形,现在我需要四舍五入矩形的角。但我找不到名为layer.cornerRadius的属性,任何人都可以帮助我?如何给UIBezierPath的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 
    } 
} 
+0

u可以使用UIBezierPath本身而绘制,使圆角半径 – vaibby

+0

由于您使用** UIBezierPath **您只需使用UIBezierPath绘制圆角。检查[this](http://ronnqvi.st/thinking-like-a-bzier-path/),它会帮助你解决所需的问题。 – iphonic

回答

6

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

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

可以请你建议圆形自定义形状,如明星。 –

1

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

希望这会有所帮助。

2

使用这种初始化

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

let path1 = UIBezierPath(roundedRect: CGRect, byRoundingCorners: UIRectCorner, cornerRadii: CGSize)) 
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 

的所有边角半径编辑本

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