iOS view圓角化的四種方法

weixin_34054866發表於2015-11-06

最近在進行專案效能的優化,遇到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;
} 

如果大家有什麼新的方法,希望推薦給我。

相關文章