iOS中設(shè)置view圓角化的四種方法示例
前言
在最近進(jìn)行項(xiàng)目性能優(yōu)化的過程中,遇到view圓角優(yōu)化的問題,有一些粗略的看法,現(xiàn)總結(jié)一下。分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。
設(shè)置圓角目前知道的有四種方法:
1、通過shapeLayer設(shè)置
2、通過view的layer設(shè)置
3、通過BezierPath設(shè)置
4、通過貼圖的方式設(shè)置
1、shapeLayer的實(shí)現(xiàn)
通過bezizerpath設(shè)置一個(gè)路徑,加到目標(biāo)視圖的layer上。代碼如下:
// 創(chuàng)建一個(gè)view UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; [self.view addSubview:showView]; showView.backgroundColor = [UIColor whiteColor]; showView.alpha = 0.5; // 貝塞爾曲線(創(chuàng)建一個(gè)圓) 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的實(shí)現(xiàn)
通過view的layer直接設(shè)置的方式,是所有的方法中最簡單的,代碼如下:
- (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的實(shí)現(xiàn)
BezierPath的實(shí)現(xiàn)方式繼承UIView,自己實(shí)現(xiàn)一個(gè)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、貼圖的實(shí)現(xiàn)
貼圖的方式是使用一個(gè)中間是圓形鏤空的圖覆蓋在需要圓角化的圖片的上方。代碼如下:
- (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; }
如果大家有什么好的方法,希望推薦給我。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
iOS開發(fā)中使用文字圖標(biāo)iconfont的應(yīng)用示例
這篇文章主要介紹了iOS開發(fā)中使用文字圖標(biāo)iconfont的應(yīng)用示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10iOS-Mac配置Tomcat教程 Mac環(huán)境配置Tomcat教程
這篇文章主要介紹了iOS-Mac配置Tomcat教程,Mac環(huán)境配置Tomcat,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11iOS App開發(fā)中使cell高度自適應(yīng)的黑魔法詳解
這篇文章主要介紹了iOS App開發(fā)中使cell高度自適應(yīng)的黑魔法詳解,作者利用iOS8以后的新特性講解了TableView、CollectionView中的cell高度自適應(yīng)以及UITextView輸入內(nèi)容實(shí)時(shí)更新cell高度的方法,需要的朋友可以參考下2016-03-03IOS UIWebView獲取404、504等錯(cuò)誤問題解決方案
這篇文章主要介紹了IOS UIWebView獲取404、504等錯(cuò)誤問題的相關(guān)資料,并對相應(yīng)的錯(cuò)誤問題提出相應(yīng)的解決方案,需要的朋友可以參考下2016-11-11