iOS设置圆角的4种方法
生活随笔
收集整理的這篇文章主要介紹了
iOS设置圆角的4种方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 直接layer層的設置(不推薦)
其中的masksToBounds會實現離屏渲染 GPU會在當前屏幕緩沖區開辟一個新的緩沖區進行工作 也就是離屏渲染 這會給我們帶來額外的性能損耗 如果這樣的圓角操作達到一定數量 會觸發緩沖區的頻繁合并和上下文的頻繁切換 性能上宏觀提現是掉幀 不建議使用 iOS9以后系統會判斷 能不產生離屏渲染的就不用了
2 貝塞爾曲線+CoreGraphics(推薦)
//創建新的位圖 //size 新位圖的大小 opaque 透明開關 scale 縮放因子 設置為0 系統自動匹配 UIGraphicsBeginImageContextWithOptions(imgTest.bounds.size, NO, 0); //用貝塞爾曲線畫一個圓形 addClip 進行切割 [[UIBezierPath bezierPathWithRoundedRect:imgTest.bounds cornerRadius:50] addClip]; //開始繪圖 [imgTest drawRect:imgTest.bounds]; imgTest.image = UIGraphicsGetImageFromCurrentImageContext(); //結束畫圖 UIGraphicsEndImageContext();3?CoreGraphics(推薦)
UIGraphicsBeginImageContextWithOptions(imgTest.bounds.size, NO, 0); //獲取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); //設置一個范圍 CGRect rect = CGRectMake(0, 0, imgTest.bounds.size.width, imgTest.bounds.size.height); //給上下文畫一個橢圓 CGContextAddEllipseInRect(ctx, rect); //裁剪 CGContextClip(ctx); //開始繪圖 [imgTest drawRect:imgTest.bounds]; imgTest.image = UIGraphicsGetImageFromCurrentImageContext(); //結束 UIGraphicsEndImageContext();4 CAShapeLayer+貝塞爾曲線(不推薦)
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imgTest.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerTopLeft cornerRadii:CGSizeMake(50, 50)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; //設置切割的范圍和路徑 maskLayer.frame = imgTest.bounds; maskLayer.path = maskPath.CGPath; mgTest.layer.mask = maskLayer; //相比之下第四種更加簡潔 但是有mask 會產生離屏渲染 //還可以規定范圍UIRectCorner /*UIRectCornerTopLeft 上左 UIRectCornerTopRight 上右 UIRectCornerBottomLeft 下左 UIRectCornerBottomRight 下右 UIRectCornerAllCorners 全部 */?
總結
以上是生活随笔為你收集整理的iOS设置圆角的4种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决宏基acer电脑开机出现start
- 下一篇: yarn npm 下载依赖报错 An u