- 为了剪切项目各种姿势的圆角,也为了让自己不要太懒了
介绍
- 剪切有layer层控件的圆角,上下左右全部.单个圆角为去实现
- 代码.h:
#import <UIKit/UIKit.h>
/**定义剪切的类型*/
typedef NS_ENUM(NSInteger, LXKShearRoundCornersType) {
LXKShearRoundCornersTypeTop = 1 << 0,
LXKShearRoundCornersTypeBottom = 1 << 1,
LXKShearRoundCornersTypeLeftRight = 1 << 2,
LXKShearRoundCornersTypeLeft = 1 << 3,
LXKShearRoundCornersTypeAll = 1 << 4,
};
/**
* 剪切有layer的圆角 可以上下左右的剪切
*/
@interface LXKShearRoundCorners : NSObject
/**
* 剪切圆角
*
* @param layer CALayer
* @param type 枚举type
* @param radiusSize radiusSize
*/
+ (void)ShearRoundCornersWidthLayer:(CALayer *)layer type:(LXKShearRoundCornersType )type radiusSize:(CGFloat)radiusSize;
@end
#import "LXKShearRoundCorners.h"
@implementation LXKShearRoundCorners
/**
* 剪切圆角
*
* @param layer CALayer
* @param type 枚举type
* @param size size
*/
+ (void)ShearRoundCornersWidthLayer:(CALayer *)layer type:(LXKShearRoundCornersType)type radiusSize:(CGFloat)radiusSize; {
UIBezierPath *maskPath;
if (type == LXKShearRoundCornersTypeTop) {
maskPath = [UIBezierPath bezierPathWithRoundedRect:layer.bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
cornerRadii:CGSizeMake(radiusSize, radiusSize)];
} else if (type == LXKShearRoundCornersTypeBottom) {
maskPath = [UIBezierPath bezierPathWithRoundedRect:layer.bounds
byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
cornerRadii:CGSizeMake(radiusSize, radiusSize)];
} else if (type == LXKShearRoundCornersTypeLeftRight) {
maskPath = [UIBezierPath bezierPathWithRoundedRect:layer.bounds
byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight)
cornerRadii:CGSizeMake(radiusSize, radiusSize)];
}else if (type == LXKShearRoundCornersTypeLeft) {
maskPath = [UIBezierPath bezierPathWithRoundedRect:layer.bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft)
cornerRadii:CGSizeMake(radiusSize, radiusSize)];
}else if (type == LXKShearRoundCornersTypeAll) {
maskPath = [UIBezierPath bezierPathWithRoundedRect:layer.bounds
byRoundingCorners:UIRectCornerAllCorners
cornerRadii:CGSizeMake(radiusSize, radiusSize)];
}
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = layer.bounds;
maskLayer.path = maskPath.CGPath;
layer.mask = maskLayer;
[layer setMasksToBounds:YES];
}
@end