美文网首页
iOS 设置圆角

iOS 设置圆角

作者: 赵哥窟 | 来源:发表于2021-06-28 09:49 被阅读0次
    截屏2021-06-28 09.44.29.png

    如图[上报]背景图右边圆角,当然切图也是可以的,写一个UIView的分类就可以实现。

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface UIView (CornerRadius)
    
    /*设置顶部圆角*/
    - (void)setCornerOnTop:(CGFloat )cornerRadius ;
    
    /*设置底部圆角*/
    - (void)setCornerOnBottom:(CGFloat )cornerRadius;
    
    /*设置左边圆角*/
    - (void)setCornerOnLeft:(CGFloat )cornerRadius;
    
    /*设置右边圆角*/
    - (void)setCornerOnRight:(CGFloat )cornerRadius;
    
    /*设置四边圆角*/
    - (void)setAllCorner;
    @end
    
    NS_ASSUME_NONNULL_END
    
    #import "UIView+CornerRadius.h"
    
    @implementation UIView (CornerRadius)
    
    /*设置顶部圆角*/
    - (void)setCornerOnTop:(CGFloat )cornerRadius {
        UIBezierPath *maskPath;
        maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
        byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
        cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }
    
    /*设置底部圆角*/
    - (void)setCornerOnBottom:(CGFloat )cornerRadius {
        UIBezierPath *maskPath;
        maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
        byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
        cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }
    
    /*设置左边圆角*/
    - (void)setCornerOnLeft:(CGFloat )cornerRadius {
        UIBezierPath *maskPath;
        maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
        byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft)
        cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }
    
    /*设置右边圆角*/
    - (void)setCornerOnRight:(CGFloat )cornerRadius {
        UIBezierPath *maskPath;
        maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
        byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight)
        cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }
    
    /*设置四边圆角*/
    - (void)setAllCorner {
        UIBezierPath *maskPath;
        maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
        cornerRadius:10.0];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }
    
    @end
    
    

    相关文章

      网友评论

          本文标题:iOS 设置圆角

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