iOS中怎么设置view圆角化,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

1、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];
2、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;
}3、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];
}4、贴图的实现
贴图的方式是使用一个中间是圆形镂空的图覆盖在需要圆角化的图片的上方。代码如下:
- (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;
}看完上述内容,你们掌握iOS中怎么设置view圆角化的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联网站建设公司行业资讯频道,感谢各位的阅读!
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。