最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS設(shè)置圓角陰影 避免離屏渲染

 更新時(shí)間:2019年04月28日 10:47:12   作者:hero_wqb  
這篇文章主要為大家詳細(xì)介紹了iOS設(shè)置圓角陰影,避免離屏渲染,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

iOS 控件設(shè)置圓角,避免離屏渲染。

離屏渲染:指GPU(圖形處理器)在當(dāng)前屏幕緩沖區(qū)外新開(kāi)辟一個(gè)渲染緩沖區(qū)進(jìn)行工作。這會(huì)給我們帶來(lái)額外的性能損耗,如果這樣的操作達(dá)到一定數(shù)量,會(huì)觸發(fā)緩沖區(qū)的頻繁合并和上下文的的頻繁切換,會(huì)出現(xiàn)卡頓、掉幀現(xiàn)象。造成離屏渲染的原因有很多,如:shouldRasterize(光柵化)、mask(遮罩層)、shadows(陰影)、EdgeAnntialiasing(抗鋸齒)、cornerRadius(圓角)等等。

下面說(shuō)一下什么情況下設(shè)置圓角會(huì)造成離屏渲染:

//設(shè)置cornerRadius>0且masksToBounds為YES
view.layer.cornerRadius = 10.f;
view.layer.masksToBounds = YES;
 
//設(shè)置cornerRadius>0且masksToBounds為YES
view.layer.cornerRadius = 10.f;
view.clipToBounds = YES;
 
//像下面設(shè)置view.layer.mask
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:imgView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10.f, 10.f)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view.bounds;
maskLayer.path = path.CGPath;
view.layer.mask = maskLayer;

上面第一種應(yīng)該是最常用的,是在設(shè)置了圓角及masksToBounds為YES時(shí),才會(huì)觸發(fā)離屏渲染,而masksToBounds默認(rèn)是NO,也就是說(shuō)只要不設(shè)置這個(gè)屬性就能避免很多情況了,下面說(shuō)一下如何切一個(gè)不觸發(fā)離屏渲染的圓角:

UIView、UITextField、UITextView等大部分控件都可以像下面這樣設(shè)置:

view.layer.cornerRadius = 10.f;
view.layer.masksToBounds = NO;

有一些特殊情況,UILabel設(shè)置時(shí),不要設(shè)置label.backgroundColor,應(yīng)設(shè)置:

label.layer.cornerRadius = 10.f;
label.layer.backgroundColor = [UIColor whiteColor].CGColor;

有圖片的UIButton、UIIMageView,用drawInRect繪制UIImage圓角:

UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
UIGraphicsBeginImageContextWithOptions(rect.size, false, [UIScreen mainScreen].scale);
CGContextAddPath(UIGraphicsGetCurrentContext(), bezierPath.CGPath);
CGContextClip(UIGraphicsGetCurrentContext());
[self drawInRect:rect];
 
CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFillStroke);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

查看離屏渲染,模擬器可以選中“Debug - Color Off-screen Rendered”開(kāi)啟調(diào)試,真機(jī)可以用Instruments檢測(cè),“Instruments - Core Animation - Debug Options - Color Offscreen-Rendered Yellow”開(kāi)啟調(diào)試,開(kāi)啟后,有離屏渲染的圖層會(huì)變成高亮的黃色。

寫(xiě)了個(gè)離屏渲染的樣例:

下面貼上代碼:

控制器ViewController:

#import "ViewController.h"
#import "UIImage+HWCorner.h"
 
#define KMainW [UIScreen mainScreen].bounds.size.width
 
@interface ViewController ()
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
 [super viewDidLoad];
 
 self.view.backgroundColor = [UIColor whiteColor];
 
 //創(chuàng)建控件
 [self creatControl];
}
 
- (void)creatControl
{
 CGFloat margin = 20.f;
 CGFloat controlW = (KMainW - margin * 3) * 0.5;
 
 NSArray *titleArray = @[@"離屏渲染", @"非離屏渲染"];
 for (int i = 0; i < titleArray.count; i++) {
 CGFloat controlX = margin + (controlW + margin) * i;
 //UILabel
 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(controlX, 30, controlW, 40)];
 label.layer.backgroundColor = [[UIColor grayColor] CGColor];
 label.text = titleArray[i];
 label.textAlignment = NSTextAlignmentCenter;
 label.layer.cornerRadius = label.bounds.size.height * 0.5;
 label.layer.masksToBounds = i == 0 ? YES : NO;
 [self.view addSubview:label];
 
 //UIView
 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(controlX, CGRectGetMaxY(label.frame) + margin, controlW, 40)];
 view.backgroundColor = [UIColor grayColor];
 view.layer.cornerRadius = view.bounds.size.height * 0.5;
 view.layer.masksToBounds = i == 0 ? YES : NO;
 [self.view addSubview:view];
 
 //UIView若未添加子控件,設(shè)置view.layer.masksToBounds = YES;也不會(huì)造成離屏渲染
 UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(30, 0, controlW - 60, 40)];
 subView.backgroundColor = [UIColor redColor];
 [view addSubview:subView];
 
 //UITextView
 UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(controlX, CGRectGetMaxY(view.frame) + margin, controlW, 40)];
 textView.userInteractionEnabled = NO;
 textView.backgroundColor = [UIColor grayColor];
 if (i == 0) {
  /*
  這里換了一種實(shí)現(xiàn)方法,用UIBezierPath賦值layer.mask,兩種方式都會(huì)造成離屏渲染
  textView.layer.cornerRadius = textView.bounds.size.height * 0.5;
  textView.layer.masksToBounds = YES;
  */
  UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:textView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(textView.bounds.size.height * 0.5, textView.bounds.size.height * 0.5)];
  CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  maskLayer.frame = textView.bounds;
  maskLayer.path = path.CGPath;
  textView.layer.mask = maskLayer;
 }else {
  textView.layer.cornerRadius = textView.bounds.size.height * 0.5;
  textView.layer.masksToBounds = NO;
 }
 [self.view addSubview:textView];
 
 //UIButton
 UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(controlX, CGRectGetMaxY(textView.frame) + margin, controlW, controlW)];
 if (i == 0) {
  [button setImage:[UIImage imageNamed:@"hero_1"] forState:UIControlStateNormal];
  button.layer.cornerRadius = button.bounds.size.width * 0.5;
  button.layer.masksToBounds = YES;
 }else {
  [button setImage:[[UIImage imageNamed:@"hero_1"] drawCornerInRect:button.bounds cornerRadius:button.bounds.size.width * 0.5] forState:UIControlStateNormal];
 }
 [self.view addSubview:button];
 
 //UIImageView設(shè)置圓角
 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(controlX, CGRectGetMaxY(button.frame) + margin, controlW, controlW)];
 if (i == 0) {
  [imageView setImage:[UIImage imageNamed:@"hero_1"]];
  imageView.layer.cornerRadius = imageView.bounds.size.width * 0.5;
  imageView.layer.masksToBounds = YES;
 }else {
  [imageView setImage:[[UIImage imageNamed:@"hero_1"] drawCornerInRect:imageView.bounds cornerRadius:imageView.bounds.size.width * 0.5]];
 }
 [self.view addSubview:imageView];
 
 //UIImageView若未添加子控件,設(shè)置imageView.layer.masksToBounds = YES;也不會(huì)造成離屏渲染
 UIView *subImageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, controlW, 40)];
 subImageView.backgroundColor = [UIColor redColor];
 subImageView.layer.cornerRadius = imageView.bounds.size.width * 0.5;
 [imageView addSubview:subImageView];
 
 //UIImageView設(shè)置陰影
 CGFloat imgW = 70.f;
 CGFloat imgPadding = (KMainW - imgW * 4 - margin * 2) / 3;
 UIImageView *shadowImgView = [[UIImageView alloc] initWithFrame:CGRectMake(margin + (imgW + imgPadding) * 2 * i, CGRectGetMaxY(imageView.frame) + margin, imgW, imgW)];
 [shadowImgView setImage:[UIImage imageNamed:@"hero_1"]];
 shadowImgView.layer.shadowColor = [UIColor redColor].CGColor;
 shadowImgView.layer.shadowOpacity = 0.8f;
 shadowImgView.layer.shadowOffset = CGSizeMake(5, 5);
 shadowImgView.layer.shadowRadius = 5.f;
 if (i == 1) {
  UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowImgView.bounds];
  shadowImgView.layer.shadowPath = path.CGPath;
 }
 [self.view addSubview:shadowImgView];
 
 //UIImageView設(shè)置陰影+圓角
 UIImageView *shadowCorImgView = [[UIImageView alloc] initWithFrame:CGRectMake(margin + imgW + imgPadding + (imgW + imgPadding) * 2 * i, CGRectGetMinY(shadowImgView.frame), imgW, imgW)];
 [shadowCorImgView setImage:[[UIImage imageNamed:@"hero_1"] drawCornerInRect:imageView.bounds cornerRadius:imageView.bounds.size.width * 0.5]];
 shadowCorImgView.layer.shadowColor = [UIColor redColor].CGColor;
 shadowCorImgView.layer.shadowOpacity = 0.8f;
 shadowCorImgView.layer.shadowOffset = CGSizeMake(0, 0);
 shadowCorImgView.layer.shadowRadius = 5.f;
 if (i == 1) {
  UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:shadowCorImgView.bounds cornerRadius:shadowCorImgView.bounds.size.height * 0.5];
  shadowCorImgView.layer.shadowPath = path.CGPath;
 }
 [self.view addSubview:shadowCorImgView];
 }
}
 
@end

UIImage分類(lèi),UIImage+HWCorner:

#import <UIKit/UIKit.h>
 
@interface UIImage (HWCorner)
 
//繪制圖片圓角
- (UIImage *)drawCornerInRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;
 
@end
 
/*** ---------------分割線--------------- ***/ 
 
#import "UIImage+HWCorner.h"
 
@implementation UIImage (HWCorner)
 
//繪制圖片圓角
- (UIImage *)drawCornerInRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius
{
 UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
 UIGraphicsBeginImageContextWithOptions(rect.size, false, [UIScreen mainScreen].scale);
 CGContextAddPath(UIGraphicsGetCurrentContext(), bezierPath.CGPath);
 CGContextClip(UIGraphicsGetCurrentContext());
 [self drawInRect:rect];
 
 CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFillStroke);
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 
 return image;
}
 
@end

最后說(shuō)一下,只有在大量的離屏渲染情況時(shí)才會(huì)出現(xiàn)卡頓、掉幀等現(xiàn)象,沒(méi)必要過(guò)分追求容不下一絲黃色。性能上Instruments工具還可以檢測(cè)很多。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS動(dòng)畫(huà)實(shí)現(xiàn)雨花與櫻花特效

    iOS動(dòng)畫(huà)實(shí)現(xiàn)雨花與櫻花特效

    小編今天為大家?guī)?lái)一場(chǎng)淅淅瀝瀝的夜空之雨和滿(mǎn)天飛舞的櫻花之戀,希望能在炎炎夏日為您帶來(lái)一絲清爽的涼意!學(xué)習(xí)iOS動(dòng)畫(huà)的小伙伴們可以參考學(xué)習(xí)。
    2016-08-08
  • iOS實(shí)現(xiàn)抖音點(diǎn)贊動(dòng)畫(huà)效果

    iOS實(shí)現(xiàn)抖音點(diǎn)贊動(dòng)畫(huà)效果

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)抖音點(diǎn)贊動(dòng)畫(huà)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • iOS藍(lán)牙開(kāi)發(fā)數(shù)據(jù)實(shí)時(shí)傳輸

    iOS藍(lán)牙開(kāi)發(fā)數(shù)據(jù)實(shí)時(shí)傳輸

    這篇文章主要為大家詳細(xì)介紹了iOS藍(lán)牙開(kāi)發(fā)數(shù)據(jù)實(shí)時(shí)傳輸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • iOS WebView中使用webp格式圖片的方法

    iOS WebView中使用webp格式圖片的方法

    由于最近項(xiàng)目需求,需要將項(xiàng)目中圖片的加載做到同時(shí)兼容WebP格式,所以下面這篇文章主要給大家介紹了關(guān)于在iOS WebView中使用webp格式圖片的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-05-05
  • IOS開(kāi)發(fā)代碼分享之獲取啟動(dòng)畫(huà)面圖片的string

    IOS開(kāi)發(fā)代碼分享之獲取啟動(dòng)畫(huà)面圖片的string

    本文是IOS開(kāi)發(fā)代碼分享系列的第一篇文章,這里分享下獲取啟動(dòng)畫(huà)面圖片的string的代碼,本代碼支持 iPhone 6 以下. 支持 iPhone 及 iPad,非常實(shí)用,希望對(duì)大家有所幫助
    2014-09-09
  • IOS開(kāi)發(fā)中使用writeToFile時(shí)的注意事項(xiàng)

    IOS開(kāi)發(fā)中使用writeToFile時(shí)的注意事項(xiàng)

    本篇文章主要介紹了開(kāi)發(fā)中使用writeToFile時(shí)的注意事項(xiàng),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-03-03
  • IOS實(shí)現(xiàn)點(diǎn)擊滑動(dòng)抽屜效果

    IOS實(shí)現(xiàn)點(diǎn)擊滑動(dòng)抽屜效果

    這篇文章主要為大家詳細(xì)介紹了IOS實(shí)現(xiàn)點(diǎn)擊滑動(dòng)抽屜效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Xcode使用教程詳細(xì)講解(全)

    Xcode使用教程詳細(xì)講解(全)

    本文介紹的是Xcode使用教程詳細(xì)講解,Xcode是一個(gè)款強(qiáng)大的IDE開(kāi)發(fā)環(huán)境,就像你在寫(xiě)Windows程序時(shí)需要VS2005一樣 需要要Xcode為你寫(xiě)Mac程序提供環(huán)境
    2015-07-07
  • IOS  Swift基礎(chǔ)之switch用法詳解

    IOS Swift基礎(chǔ)之switch用法詳解

    這篇文章主要介紹了IOS Swift基礎(chǔ)之switch用法詳解的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • ios系統(tǒng)下刪除文件的代碼

    ios系統(tǒng)下刪除文件的代碼

    本文給大家總結(jié)了幾則在IOS系統(tǒng)下刪除文件的代碼,十分的實(shí)用,有需要的小伙伴可以參考下。
    2015-05-05

最新評(píng)論

商丘市| 光泽县| 南汇区| 通许县| 万年县| 丹棱县| 沾益县| 玛曲县| 施甸县| 重庆市| 陆河县| 长汀县| 承德市| 丽江市| 南昌县| 怀仁县| 陇南市| 保靖县| 梅河口市| 康保县| 黔东| 宜君县| 日照市| 广汉市| 阳高县| 海南省| 承德县| 商河县| 苍溪县| 崇礼县| 仙居县| 双流县| 曲周县| 如东县| 茂名市| 芜湖县| 宜君县| 兴国县| 蓝田县| 保德县| 丹阳市|