溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

iOS中怎么設置view圓角化

發(fā)布時間:2021-07-22 16:59:16 來源:億速云 閱讀:298 作者:Leah 欄目:移動開發(fā)

iOS中怎么設置view圓角化,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

1、shapeLayer的實現(xiàn)

通過bezizerpath設置一個路徑,加到目標視圖的layer上。代碼如下:

// 創(chuàng)建一個view
 UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
 [self.view addSubview:showView];
 showView.backgroundColor = [UIColor whiteColor];
 showView.alpha = 0.5;
 
 // 貝塞爾曲線(創(chuàng)建一個圓)
 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];

2、view的layer的實現(xiàn)

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

3、BezierPath的實現(xiàn)

BezierPath的實現(xiàn)方式繼承UIView,自己實現(xiàn)一個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];
}

4、貼圖的實現(xiàn)

貼圖的方式是使用一個中間是圓形鏤空的圖覆蓋在需要圓角化的圖片的上方。代碼如下:

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

看完上述內(nèi)容,你們掌握iOS中怎么設置view圓角化的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI