iOS設(shè)置圓角的三種方法
更新時間:2017年10月19日 14:02:51 作者:路人Q
這篇文章主要為大家詳細介紹了iOS設(shè)置圓角的三種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了iOS設(shè)置圓角的三種方式,供大家參考,具體內(nèi)容如下
第一種方法:通過設(shè)置layer的屬性
最簡單的一種,但是很影響性能,一般在正常的開發(fā)中使用很少.
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; //只需要設(shè)置layer層的兩個屬性 //設(shè)置圓角 imageView.layer.cornerRadius = imageView.frame.size.width / 2; //將多余的部分切掉 imageView.layer.masksToBounds = YES; [self.view addSubview:imageView];
第二種方法:使用貝塞爾曲線UIBezierPath和Core Graphics框架畫出一個圓角
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; imageView.image = [UIImage imageNamed:@"1"]; //開始對imageView進行畫圖 UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0); //使用貝塞爾曲線畫出一個圓形圖 [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip]; [imageView drawRect:imageView.bounds]; imageView.image = UIGraphicsGetImageFromCurrentImageContext(); //結(jié)束畫圖 UIGraphicsEndImageContext(); [self.view addSubview:imageView];
第三種方法:使用CAShapeLayer和UIBezierPath設(shè)置圓角
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 100)]; imageView.image = [UIImage imageNamed:@"1"]; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft cornerRadii:CGSizeMake(25, 5)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init]; maskLayer.frame = imageView.bounds; maskLayer.path = maskPath.CGPath; imageView.layer.mask = maskLayer; [self.view addSubview:imageView]; }
這三種方法中第三種最好,對內(nèi)存的消耗最少啊,而且渲染快速。
效果圖:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS tableView上拉刷新顯示下載進度的問題及解決辦法
這篇文章主要介紹了 iOS tableView上拉刷新顯示下載進度的問題及解決辦法,需要的朋友可以參考下2017-03-03iOS UICollectionView實現(xiàn)橫向滑動
這篇文章主要為大家詳細介紹了iOS UICollectionView實現(xiàn)橫向滑動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03