2016-01-28 18 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]