溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

iOS如何設(shè)置圓角

發(fā)布時(shí)間:2021-07-30 11:43:38 來源:億速云 閱讀:149 作者:小新 欄目:移動(dòng)開發(fā)

這篇文章主要為大家展示了“iOS如何設(shè)置圓角”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“iOS如何設(shè)置圓角”這篇文章吧。

第一種方法:通過設(shè)置layer的屬性

最簡(jiǎn)單的一種,但是很影響性能,一般在正常的開發(fā)中使用很少.

  UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
  //只需要設(shè)置layer層的兩個(gè)屬性
  //設(shè)置圓角
  imageView.layer.cornerRadius = imageView.frame.size.width / 2;
  //將多余的部分切掉
  imageView.layer.masksToBounds = YES;
  [self.view addSubview:imageView];

第二種方法:使用貝塞爾曲線UIBezierPath和Core Graphics框架畫出一個(gè)圓角

 UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
  imageView.image = [UIImage imageNamed:@"1"];
  //開始對(duì)imageView進(jìn)行畫圖
  UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
  //使用貝塞爾曲線畫出一個(gè)圓形圖
  [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];
  [imageView drawRect:imageView.bounds];
  imageView.image = UIGraphicsGetImageFromCurrentImageContext();
   //結(jié)束畫圖
  UIGraphicsEndImageContext();
  [self.view addSubview:imageView];

第三種方法:使用CAShapeLayer和UIBezierPath設(shè)置圓角

首先需要導(dǎo)入<AVFoundation/AVFoundation.h>

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
  imageView.image = [UIImage imageNamed:@"1"];
  UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageView.bounds.size];
  CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
  //設(shè)置大小
  maskLayer.frame = imageView.bounds;
  //設(shè)置圖形樣子
  maskLayer.path = maskPath.CGPath;
  imageView.layer.mask = maskLayer;
  [self.view addSubview:imageView];
}

這三種方法中第三種最好,對(duì)內(nèi)存的消耗最少啊,而且渲染快速。

以上是“iOS如何設(shè)置圓角”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

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

ios
AI