当前位置:首页 > 通信资讯 > 正文

给view设置圆角(ios 自定义view)

前言

在最近进行项目性能优化的过程中,遇到view圆角优化的问题,有一些粗略的看法,现总结一下。分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

设置圆角目前知道的有四种方法:

1、通过shapeLayer设置

2、通过view的layer设置

3、通过BezierPath设置

4、通过贴图的方式设置

1、shapeLayer的实现

通过bezizerpath设置一个路径,加到目标视图的layer上。代码如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // 创建一个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];

2、view的layer的实现

通过view的layer直接设置的方式,是所有的方法中最简单的,代码如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 - (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; }

3、BezierPath的实现

BezierPath的实现方式继承UIView,自己实现一个customview,代码如下。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 - (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]; }

4、贴图的实现

贴图的方式是使用一个中间是圆形镂空的图覆盖在需要圆角化的图片的上方。代码如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 - (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; }

如果大家有什么好的方法,希望推荐给我。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://segmentfault.com/a/1190000010151641

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。