一般情況下我們給UIKit控件添加圓角都是改變clipsToBounds和layer.cornerRadius,這樣雖然看似簡(jiǎn)單虱而,兩行代碼就可以搞定霞势。但是,這樣使用會(huì)強(qiáng)制Core Animation提前渲染屏幕的離屏繪制患整,而離屏繪制就會(huì)為性能帶來(lái)負(fù)面影響眯停。
下面為大家提供一種比較復(fù)雜的方式來(lái)為控件添加圓角:
#import <UIKit/UIKit.h>
@interface UIView (RoundedCorner)
// 設(shè)置頂部圓角
- (void)setRoundedCornerOnTop;
// 設(shè)置底部圓角
- (void)setRoundedCornerOnBottom;
// 設(shè)置圓角
- (void)setRoundedCorner;
// 取消圓角
- (void)removeRoundedCorner;
// 自定義圓角大小 scale范圍:0 ~ 0.5
- (void)setRoundedCornerWithScale:(CGFloat)scale;
// 自定義頂部圓角大小 scale范圍:0 ~ 0.5
- (void)setRoundedCornerOnTopWithScale:(CGFloat)scale;
// 自定義底部圓角大小 scale范圍:0 ~ 0.5
- (void)setRoundedCornerOnBottomWithScale:(CGFloat)scale;
@end
#import "UIView+RoundedCorner.h"
@implementation UIView (RoundedCorner)
- (void)setRoundedCornerOnTop {
[self setRoundedCornerOnTopWithScale:0.5];
}
- (void)setRoundedCornerOnBottom {
[self setRoundedCornerOnBottomWithScale:0.5];
}
- (void)setRoundedCorner {
[self setRoundedCornerWithScale:0.5];
}
- (void)removeRoundedCorner {
self.layer.mask = nil;
}
- (void)setRoundedCornerWithScale:(CGFloat)scale {
if (scale > 0.5) {
scale = 0.5;
}
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.frame.size.height * scale];
[self setShapeLayer:maskPath];
}
- (void)setRoundedCornerOnTopWithScale:(CGFloat)scale {
if (scale > 0.5) {
scale = 0.5;
}
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(self.frame.size.height * scale, self.frame.size.height * scale)];
[self setShapeLayer:maskPath];
}
- (void)setRoundedCornerOnBottomWithScale:(CGFloat)scale {
if (scale > 0.5) {
scale = 0.5;
}
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(self.frame.size.height * scale, self.frame.size.height * scale)];
[self setShapeLayer:maskPath];
}
// 創(chuàng)建CAShapeLayer并設(shè)置
- (void)setShapeLayer:(UIBezierPath *)maskPath {
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
@end
當(dāng)然還有給UIImageView添加圓角的方法济舆。(注意:如果加載的是網(wǎng)絡(luò)圖片,必須要等網(wǎng)絡(luò)圖片加載完成再調(diào)用)
#import <UIKit/UIKit.h>
@interface UIImageView (RoundedCorner)
- (UIImageView *)imageViewWithRoundedCornerSize:(CGFloat)cornerRadius;
@end
#import "UIImageView+RoundedCorner.h"
@implementation UIImageView (RoundedCorner)
- (UIImageView *)imageViewWithRoundedCornerSize:(CGFloat)cornerRadius {
UIImageView *imageView = self;
UIImage *original = self.image;
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:cornerRadius] addClip];
[original drawInRect:imageView.bounds];
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageView;
}
@end
代碼Demo點(diǎn)這里:http://git.oschina.net/chocklee/RoundedCorner