美文网首页
设置view的圆角的类别category

设置view的圆角的类别category

作者: 伏特加 | 来源:发表于2016-02-22 14:27 被阅读80次

    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
    

    相关文章

      网友评论

          本文标题:设置view的圆角的类别category

          本文链接:https://www.haomeiwen.com/subject/lvrbkttx.html