@property(nonatomic) BOOL clipsToBounds; // When YES, content and subviews are clipped to the bounds of the view. Default is NO.
@property CGFloat cornerRadius;
/* Defines which of the four corners receives the masking when using
* `cornerRadius' property. Defaults to all four corners. */
#import
#pragma mark - Associated Object
- (void)setFf_shapeLayer:(id)object{
objc_setAssociatedObject(self, @selector(ff_shapeLayer), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CAShapeLayer *)ff_shapeLayer{
return objc_getAssociatedObject(self, @selector(ff_shapeLayer));
}
- (void)setFf_bezierPath:(id)object{
objc_setAssociatedObject(self, @selector(ff_bezierPath), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIBezierPath *)ff_bezierPath{
return objc_getAssociatedObject(self, @selector(ff_bezierPath));
}
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};
/*
@paramater。必须拿到坐标后再使用
*/
#import
@interface UIView (SignView)
/**
继承于UIView的视图切圆角
@param cornerType 圆角类型, 可看系统自带类型
@param radius 圆角角度
*/
- (void)ff_setCornerType:(UIRectCorner)cornerType
cornerRadius:(CGFloat)radius;
/**
继承于UIView的视图切圆角
@param cornerType 圆角类型, 可看系统自带类型
@param resize CGSize类型剪切
*/
- (void)ff_setCornerType:(UIRectCorner)cornerType
cornerSizeRadius:(CGSize)resize;
/**
@warning 用于特殊需求
继承于UIView的视图切圆角
@param rect 当前控件frame
@param cornerType 剪切类型
@param resize size大小
*/
- (void)ff_setRoundedRect:(CGRect)rect
CornerType:(UIRectCorner)cornerType
cornerSizeRadius:(CGSize)resize;
@end
#import "UIView+SignView.h"
#import
@implementation UIView (SignView)
- (void)ff_setCornerType:(UIRectCorner)cornerType
cornerRadius:(CGFloat)radius{
CGSize size = CGSizeMake(self.frame.size.width, radius);
[self sign_cornerType:cornerType cornerSizeRadius:size];
}
- (void)ff_setCornerType:(UIRectCorner)cornerType
cornerSizeRadius:(CGSize)resize{
[self sign_cornerType:cornerType cornerSizeRadius:resize];
}
- (void)ff_setRoundedRect:(CGRect)rect
CornerType:(UIRectCorner)cornerType
cornerSizeRadius:(CGSize)resize{
self.ff_bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect
byRoundingCorners:cornerType
cornerRadii:CGSizeMake(resize.width, resize.height)];
self.ff_shapeLayer = [self get_shapeLayer];
self.ff_shapeLayer.path = self.ff_bezierPath.CGPath;
self.layer.mask = self.ff_shapeLayer;
}
- (void)sign_cornerType:(UIRectCorner)cornerType
cornerSizeRadius:(CGSize)size{
NSLog(@"width :%f height : %f",self.frame.size.width,self.frame.size.height);
self.ff_bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)
byRoundingCorners:cornerType
cornerRadii:CGSizeMake(size.width, size.height)];
self.ff_shapeLayer = [self get_shapeLayer];
self.ff_shapeLayer.path = self.ff_bezierPath.CGPath;
self.layer.mask = self.ff_shapeLayer;
}
- (CAShapeLayer *)get_shapeLayer{
CAShapeLayer * layers = [CAShapeLayer layer];
layers.frame = self.bounds;
return layers;
}
#pragma mark - Associated Object
- (void)setFf_shapeLayer:(id)object{
objc_setAssociatedObject(self, @selector(ff_shapeLayer), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CAShapeLayer *)ff_shapeLayer{
return objc_getAssociatedObject(self, @selector(ff_shapeLayer));
}
- (void)setFf_bezierPath:(id)object{
objc_setAssociatedObject(self, @selector(ff_bezierPath), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIBezierPath *)ff_bezierPath{
return objc_getAssociatedObject(self, @selector(ff_bezierPath));
}
声明属性
@property (nonatomic, strong) UIImageView * leftClipImgeView; // 只剪切左上圆角
#pragma 只剪切左上圆角 Getter Method 懒加载
- (UIImageView *)leftClipImgeView{
if (!_leftClipImgeView) {
_leftClipImgeView = [[UIImageView alloc]init];
_leftClipImgeView.image = [UIImage imageNamed:@"m8.jpg"];
}
return _leftClipImgeView;
}
// 只剪切左上圆角
- (void)clipLeftCornerRadiu{
[self.view addSubview:self.leftClipImgeView];
self.leftClipImgeView.frame = CGRectMake(CGRectGetMaxX(self.bgImageView.frame)+marginWidth, CGRectGetMinY(self.bgImageView.frame), ViewHeight, ViewHeight);
[self.leftClipImgeView ff_setCornerType:UIRectCornerTopLeft
cornerRadius:ViewHeight/2];
}
- (void)viewDidLoad {
[super viewDidLoad];
// 切4个角
[self clipAllCornerRadius];
// 只剪切左上圆角
[self clipLeftCornerRadiu];
// 只剪切右上圆角
[self clipRightCornerRadiu];
// 只剪切左下圆角
[self clipBottomLeftCornerRadiu];
// 只剪切右下圆角
[self clipBottomRIghtCornerRadiu];
// 只剪切上左,上右圆角
[self clipTopCornerRadius];
// 只剪切下左,下右圆角
[self clipBottomCornerRadius];
// 不规则剪切
[self clipCornerRadius];
}
Demo地址