iOS切圓角的方式有三種
1.?通過設置layer的屬性
最簡單的一種,但是很影響性能,一般在正常的開發(fā)中使用很少.
self.button.layer.cornerRadius =30;
self.button.layer.masksToBounds= YES;
2.使用貝塞爾曲線UIBezierPath和Core Graphics 使用不多
貌似只能使用ImageView 這個不太懂
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100,100,100,100)];
imageView.center=self.view.center;
imageView.image= [UIImage imageNamed:@"1"];//開始對imageView進行畫圖UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, [UIScreen mainScreen].scale);//使用貝塞爾曲線畫出一個圓形圖[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];
[imageView drawRect:imageView.bounds];
imageView.image=UIGraphicsGetImageFromCurrentImageContext();
UIImage*image =UIGraphicsGetImageFromCurrentImageContext();
[self.button setImage:image forState:UIControlStateNormal];//結束畫圖UIGraphicsEndImageContext();
[self.view addSubview:imageView];
3.使用CAShapeLayer和UIBezierPath設置圓角
// 只切一個角//? ? UIBezierPath *maskPAth = [UIBezierPath bezierPathWithRoundedRect:self.button.bounds byRoundingCorners:UIRectCornerTopLeft cornerRadii:self.button.bounds.size];? // 只切兩個角//? ? UIBezierPath *maskPAth = [UIBezierPath bezierPathWithRoundedRect:self.button.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];? ? ? ? // 注意 當size是整個大小時 切角是對角線會切成圓? size不是整個視圖大小不會成圓角有人說必須導入我是沒有發(fā)現(xiàn) 如果不行可以導入試試
//? ? UIBezierPath *maskPAth = [UIBezierPath bezierPathWithRoundedRect:self.button.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:self.button.bounds.size];
//? ? UIBezierPath *maskPAth = [UIBezierPath bezierPathWithRoundedRect:self.button.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
// 三個角
//? ? UIBezierPath *maskPAth = [UIBezierPath bezierPathWithRoundedRect:self.button.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight | UIRectCornerTopRight cornerRadii:CGSizeMake(30, 30)];
// 四個角
//? ? UIBezierPath *maskPAth = [UIBezierPath bezierPathWithRoundedRect:self.button.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:self.button.bounds.size];
//? ? UIBezierPath *maskPAth = [UIBezierPath bezierPathWithRoundedRect:self.button.bounds cornerRadius:10];
//? ? CAShapeLayer *caShapeLayer = [CAShapeLayer layer];
//? ? caShapeLayer.path = maskPAth.CGPath;
//? ? self.button.layer.mask = caShapeLayer;
這三種方法中第三種最好香缺,對內(nèi)存的消耗最少啊,而且渲染快速兽赁。
注意:這三種方法都是顯示的是圓角伶丐,并不是真實切除角,在debug viewhierarchy上可以看出大小不變 顯示改變