平常設置視圖的圓角最普遍的就是設置四個角的,方法也就是一句代碼解決:
view.layer.cornerRadius = 10;
四個圓角
但有時需求會是指定某個,或者特定哪幾個角設置圓角,所以我們需要不一樣的解決方法:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(50, 150, 60, 60)];
view.backgroundColor = [UIColor orangeColor];
// UIRectCornerTopLeft 左上角
// UIRectCornerTopRight 右上角
// UIRectCornerBottomLeft 左下角
// UIRectCornerBottomRight 右下角
// UIRectCornerAllCorners 四個角
// byRoundingCorners: 參數可以選擇上面五種,需要制定某幾個角為圓角,就要用 “|” 組合
// cornerRadii: 代表圓角值大小
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view.bounds;
maskLayer.path = maskPath.CGPath;
view.layer.mask = maskLayer;
[self.view addSubview:view];
左上和右下
此方法可以適用很多種視圖,比如UIView
、UILabel
、UIImageView
、UIButton
等。