你可以這樣說:
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.viewOutlet.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path = maskPath.CGPath;
self.viewOutlet.layer.mask = maskLayer;
更新:
如果您需要邊框只需創建另一個CAShapeLayer
並將其添加到視圖的圖層作爲子圖層。像這樣(的地方下面上的代碼的代碼):
CAShapeLayer *borderLayer = [[CAShapeLayer alloc] init];
borderLayer.frame = self.view.bounds;
borderLayer.path = maskPath.CGPath;
borderLayer.lineWidth = 4.0f;
borderLayer.strokeColor = [UIColor blackColor].CGColor;
borderLayer.fillColor = [UIColor clearColor].CGColor;
[self.viewOutlet.layer addSublayer:borderLayer];
在SWIFT 3.0這樣的:
let maskPath = UIBezierPath.init(roundedRect: self.viewOutlet.bounds, byRoundingCorners:[.topLeft, .bottomLeft], cornerRadii: CGSize.init(width: 10.0, height: 10.0))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.viewOutlet.bounds
maskLayer.path = maskPath.cgPath
self.viewOutlet.layer.mask = maskLayer
嘗試使用'UIBezierPath * maskPath = [UIBezierPath新];'然後用'addLineToPoint:','addArcWithCenter:半徑:由startAngle:endAngle:順時針方向:'方法。 – 2014-09-02 05:42:23
你的代碼似乎沒問題。只需檢查您是否添加了QuartzCore框架並在您正在編寫此代碼的類中導入。我不知道情況確實如此,但早些時候發生在我身上。 – 2014-09-02 06:22:31
可能的重複[Rounded UIView使用CALayers - 只有一些角落 - 如何?](http://stackoverflow.com/questions/2264083/round-uiview-using-calayers-only-some-corners-how) – Stuart 2014-09-02 06:34:53