您的位置:首页 > 产品设计 > UI/UE

iOS开发之指定UIView的某几个角为圆角

2016-04-20 12:19 573 查看
如果需要将UIView的4个角全部都为圆角,做法相当简单,只需设置其Layer的cornerRadius属性即可(项目需要使用QuartzCore框架)。而若要指定某几个角(小于4)为圆角而别的不变时,这种方法就不好用了。

一种简单的实现方式 ,用到CAShapeLayer

UIView *redView = [[UIView
alloc] initWithFrame:CGRectMake(120,
300, 80,
80)];

redView.backgroundColor = [UIColor
redColor];
[self.view
addSubview:redView];

UIBezierPath *maskPath = [UIBezierPath
bezierPathWithRoundedRect:redView.bounds
byRoundingCorners:UIRectCornerBottomLeft |
UIRectCornerBottomRight cornerRadii:CGSizeMake(10,
10)];

// [UIBezierPath bezierPathWithRoundedRect:<#(CGRect)#> byRoundingCorners:<#(UIRectCorner)#> cornerRadii:<#(CGSize)#>]

// typedef NS_OPTIONS(NSUInteger, UIRectCorner) {

// UIRectCornerTopLeft = 1 << 0,

// UIRectCornerTopRight = 1 << 1,

// UIRectCornerBottomLeft = 1 << 2,

// UIRectCornerBottomRight = 1 << 3,

// UIRectCornerAllCorners = ~0UL

// };可以选择任意几个角

// cornerRadii 圆角的大小

CAShapeLayer *maskLayer = [[CAShapeLayer
alloc] init];
maskLayer.frame = redView.bounds;
maskLayer.path = maskPath.CGPath;
redView.layer.mask = maskLayer;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: