美文网首页iOS开发
OC-设置不同边的圆角

OC-设置不同边的圆角

作者: 紫云夕月 | 来源:发表于2021-08-30 16:33 被阅读0次
    typedef NS_ENUM(NSInteger,UILayoutCornerRadiusType) {
        UILayoutCornerRadiusTop = 0,
        UILayoutCornerRadiusLeft = 1,
        UILayoutCornerRadiusBottom = 2,
        UILayoutCornerRadiusRight = 3,
        UILayoutCornerRadiusAll = 4,
        UILayoutCornerdeleteTopleft = 5,
    };
    
    @interface UIView (CornerRadius)
    /**
     
     @author
     设置不同边的圆角
     @param radiusType 圆角类型
     @param cornerRadius 圆角半径
     */
    -(void)UILayoutCornerRadiusType:(UILayoutCornerRadiusType)radiusType withCornerRadius:(CGFloat)cornerRadius;
    @end
    
    #import "UIView+CornerRadius.h"
    
    @implementation UIView (CornerRadius)
    
    -(void)UILayoutCornerRadiusType:(UILayoutCornerRadiusType)sideType withCornerRadius:(CGFloat)cornerRadius
    {
        
        CGSize cornerSize = CGSizeMake(cornerRadius, cornerRadius);
        UIBezierPath *maskPath;
        
        switch (sideType) {
            case UILayoutCornerRadiusTop:
            {
                maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                 byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
                                                       cornerRadii:cornerSize];
            }
                break;
            case UILayoutCornerRadiusLeft:
            {
                maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                 byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerBottomLeft)
                                                       cornerRadii:cornerSize];
            }
                break;
            case UILayoutCornerRadiusBottom:
            {
                maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                 byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight)
                                                       cornerRadii:cornerSize];
            }
                break;
            case UILayoutCornerRadiusRight:
            {
                maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                 byRoundingCorners:(UIRectCornerTopRight|UIRectCornerBottomRight)
                                                       cornerRadii:cornerSize];
            }
                break;
            case UILayoutCornerdeleteTopleft:
            {
                maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                 byRoundingCorners:(UIRectCornerTopRight|UIRectCornerBottomRight|UIRectCornerBottomLeft)
                                                       cornerRadii:cornerSize];
            }
                break;
            default:
            {
                maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                 byRoundingCorners:UIRectCornerAllCorners
                                                       cornerRadii:cornerSize];
            }
                break;
        }
        
        CAShapeLayer * maskLayer = [CAShapeLayer layer];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        
        self.layer.mask = maskLayer;
        
        [self.layer setMasksToBounds:YES];
    }
    @end
    

    相关文章

      网友评论

        本文标题:OC-设置不同边的圆角

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