###iOS 开发切圆之 View 只切指定的角
如果需要将UIView的4个角全部都为圆角,做法相当简单,只需设置其Layer的cornerRadius属性即可(项目需要使用QuartzCore框架)。而若要指定某几个角(小于4)为圆角而别的不变时,这种方法就不好用了。
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 10, 80, 80)];
view2.backgroundColor = [UIColor redColor];
[self.view addSubview:view2];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view2.bounds;
maskLayer.path = maskPath.CGPath;
view2.layer.mask = maskLayer;
其中
byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight
指定了需要成为圆角的角。该参数是UIRectCorner类型的,可选的值有
* UIRectCornerTopLeft
* UIRectCornerTopRight
* UIRectCornerBottomLeft
* UIRectCornerBottomRight
* UIRectCornerAllCorners
从名字很容易看出来代表的意思,使用“|”来组合就好了。
#####view的layer的实现
通过view的layer直接设置的方式,是所有的方法中最简单的,代码如下:
- (UIImageView *)avatarImage {
if (!_avatarImage) {
_avatarImage = [[UIImageView alloc] initWithFrame:CGRectMake(20,10, avatarDiameter, avatarDiameter)];
_avatarImage.backgroundColor = [UIColor grayColor];
_avatarImage.contentMode = UIViewContentModeScaleAspectFit;
_avatarImage.layer.cornerRadius = avatarDiameter/2.0;
_avatarImage.layer.masksToBounds = YES;
[_avatarImage setImage:[UIImage imageNamed:@"test.jpg"]];
}
return _avatarImage;
}
#####shapeLayer的实现
通过bezizerpath设置一个路径,加到目标视图的layer上。代码如下:
// 创建一个view
UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:showView];
showView.backgroundColor = [UIColor whiteColor];
showView.alpha = 0.5;
// 贝塞尔曲线(创建一个圆)
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100 / 2.f, 100 / 2.f)
radius:100 / 2.f
startAngle:0
endAngle:M_PI * 2
clockwise:YES];
CAShapeLayer *layer = [CAShapeLayer layer];
layer.frame = showView.bounds;
layer.path = path.CGPath;
[showView.layer addSublayer:layer];
#####BezierPath的实现
BezierPath的实现方式继承UIView,自己实现一个customview,代码如下。
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code
CGRect bounds = self.bounds;
[[UIColor whiteColor] set];
UIRectFill(bounds);
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:CGRectGetWidth(bounds)/2.0] addClip];
[self.image drawInRect:bounds];
}
#####贴图的实现
贴图的方式是使用一个中间是圆形镂空的图覆盖在需要圆角化的图片的上方。代码如下:
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self.contentView addSubview:self.avatarImage];
[self.contentView addSubview:self.maskImage];
}
return self;
}
- (UIImageView *)avatarImage {
if (!_avatarImage) {
_avatarImage = [[UIImageView alloc] initWithFrame:CGRectMake(20,10, avatarDiameter, avatarDiameter)];
_avatarImage.backgroundColor = [UIColor grayColor];
_avatarImage.contentMode = UIViewContentModeScaleAspectFit;
[_avatarImage setImage:[UIImage imageNamed:@"test.jpg"]];
}
return _avatarImage;
}
//中心镂空的图
- (UIImageView *)maskImage {
if (!_maskImage) {
_maskImage = [[UIImageView alloc] initWithFrame:CGRectMake(20,10, avatarDiameter, avatarDiameter)];
_maskImage.contentMode = UIViewContentModeScaleAspectFit;
[_maskImage setImage:[UIImage imageNamed:@"corner_circle.png"]];
}
return _maskImage;
}