UIView+CornerRadius.h
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger,CornerType){
CornerTypeTopLeft =0,
CornerTypeTopRight,
CornerTypeBottomLeft,
CornerTypeBottomRight,
CornerTypeTop,
CornerTypeLeft,
CornerTypeRight,
CornerTypeBottom,
CornerTypeNoTopLeft,
CornerTypeNoTopRight,
CornerTypeNoBottomLeft,
CornerTypeNoBottomRight,
CornerTypeAll
};
@interface UIView (CornerRadius)
- (void)setCornerType:(CornerType)type cornerRadius:(CGFloat)cornerRadius;
@end
UIView+CornerRadius.m
#import "UIView+CornerRadius.h"
@implementation UIView (CornerRadius)
- (void)setCornerType:(CornerType)type cornerRadius:(CGFloat)cornerRadius
{
CGSize cornerSize = CGSizeMake(cornerRadius, cornerRadius);
UIRectCorner corner;
switch (type) {
case CornerTypeTopLeft:
corner = UIRectCornerTopLeft;
break;
case CornerTypeTopRight:
corner = UIRectCornerTopRight;
break;
case CornerTypeBottomLeft:
corner = UIRectCornerBottomLeft;
break;
case CornerTypeBottomRight:
corner = UIRectCornerBottomRight;
break;
case CornerTypeTop:
corner = UIRectCornerTopRight|UIRectCornerTopLeft;
break;
case CornerTypeLeft:
corner = UIRectCornerTopLeft|UIRectCornerBottomLeft;
break;
case CornerTypeBottom:
corner = UIRectCornerBottomLeft|UIRectCornerBottomRight;
break;
case CornerTypeRight:
corner = UIRectCornerBottomRight|UIRectCornerTopRight;
break;
case CornerTypeNoTopLeft:
corner = UIRectCornerBottomLeft|UIRectCornerTopRight|UIRectCornerBottomRight;
break;
case CornerTypeNoTopRight:
corner = UIRectCornerBottomRight|UIRectCornerBottomLeft|UIRectCornerTopLeft;
break;
case CornerTypeNoBottomLeft:
corner = UIRectCornerTopLeft|UIRectCornerBottomRight|UIRectCornerTopRight;
break;
case CornerTypeNoBottomRight:
corner = UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft;
break;
case CornerTypeAll:
corner = UIRectCornerAllCorners;
break;
default:
break;
}
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corner cornerRadii:cornerSize];
CAShapeLayer *layer = [[CAShapeLayer alloc]init];
layer.path = path.CGPath;
layer.frame = self.bounds;
self.layer.mask = layer;
}
@end
网友评论