iOS中設置view圓角化的四種方法示例
前言
在最近進行項目性能優(yōu)化的過程中,遇到view圓角優(yōu)化的問題,有一些粗略的看法,現(xiàn)總結一下。分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。
設置圓角目前知道的有四種方法:
1、通過shapeLayer設置
2、通過view的layer設置
3、通過BezierPath設置
4、通過貼圖的方式設置
1、shapeLayer的實現(xiàn)
通過bezizerpath設置一個路徑,加到目標視圖的layer上。代碼如下:
// 創(chuàng)建一個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)建一個圓) 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的實現(xiàn)
通過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的實現(xiàn)
BezierPath的實現(xiàn)方式繼承UIView,自己實現(xiàn)一個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、貼圖的實現(xiàn)
貼圖的方式是使用一個中間是圓形鏤空的圖覆蓋在需要圓角化的圖片的上方。代碼如下:
- (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-Mac配置Tomcat教程 Mac環(huán)境配置Tomcat教程
這篇文章主要介紹了iOS-Mac配置Tomcat教程,Mac環(huán)境配置Tomcat,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11iOS App開發(fā)中使cell高度自適應的黑魔法詳解
這篇文章主要介紹了iOS App開發(fā)中使cell高度自適應的黑魔法詳解,作者利用iOS8以后的新特性講解了TableView、CollectionView中的cell高度自適應以及UITextView輸入內容實時更新cell高度的方法,需要的朋友可以參考下2016-03-03IOS UIWebView獲取404、504等錯誤問題解決方案
這篇文章主要介紹了IOS UIWebView獲取404、504等錯誤問題的相關資料,并對相應的錯誤問題提出相應的解決方案,需要的朋友可以參考下2016-11-11