iOS 设置圆角

2021-06-29 10:40:55 浏览数 (1)

截屏2021-06-28 09.44.29.png

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

代码语言:javascript复制
#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
代码语言:javascript复制
#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

0 人点赞