剪切iOS开发中的圆角

2016-08-03  本文已影响148人  LennonLin

介绍

#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

参考地址

上一篇 下一篇

猜你喜欢

热点阅读