溫馨提示×

溫馨提示×

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

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

iOS如何設置圓角

發布時間:2021-07-30 11:43:38 來源:億速云 閱讀:170 作者:小新 欄目:移動開發

這篇文章主要為大家展示了“iOS如何設置圓角”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“iOS如何設置圓角”這篇文章吧。

第一種方法:通過設置layer的屬性

最簡單的一種,但是很影響性能,一般在正常的開發中使用很少.

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

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

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

第三種方法:使用CAShapeLayer和UIBezierPath設置圓角

首先需要導入<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];
  //設置大小
  maskLayer.frame = imageView.bounds;
  //設置圖形樣子
  maskLayer.path = maskPath.CGPath;
  imageView.layer.mask = maskLayer;
  [self.view addSubview:imageView];
}

這三種方法中第三種最好,對內存的消耗最少啊,而且渲染快速。

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

向AI問一下細節

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

ios
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女