您好,登錄后才能下訂單哦!
iOS中怎么設置view圓角化,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
1、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];
2、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; }
3、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]; }
4、貼圖的實現
貼圖的方式是使用一個中間是圓形鏤空的圖覆蓋在需要圓角化的圖片的上方。代碼如下:
- (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; }
看完上述內容,你們掌握iOS中怎么設置view圓角化的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。