iOS view圆角化的四种方法

最近在进行项目性能的优化,遇到view圆角优化的问题,有一些粗略的看法,现总结一下。

设置圆角目前知道的有四种方法:

1、通过shapeLayer设置
2、通过view的layer设置
3、通过BezierPath设置
4、通过贴图的方式设置
shapeLayer的实现

通过bezizerpath设置一个路径,加到目标视图的layer上。代码如下:

 // 创建一个view
  UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
  [self.view addSubview:showView];
  showView.backgroundColor = [UIColor whiteColor];
  showView.alpha = 0.5;

   // 贝塞尔曲线(创建一个圆)
    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];

view的layer的实现

通过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;
}

BezierPath的实现

BezierPath的实现方式继承UIView,自己实现一个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];
}

贴图的实现

贴图的方式是使用一个中间是圆形镂空的图覆盖在需要圆角化的图片的上方。代码如下:

- (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;
}

PS

:还有一种,直接在image上面进行处理。遍历像素点,把圆角之外的像素设置透明
具体参考这个:iOS开发-定制多样式二维码

你可能感兴趣的:(view,iOS常用小功能)