UIView任意角设置 cornerRedius

设置UIView的圆角这个相信大家都知道怎么做,但有时项目需求是实现某个角的圆角,好的,下面我们就来看看怎么实现吧。

构建一个基于UIView的类别

#import <UIKit/UIKit.h>
/**
 *  View 圆角
 */
@interface UIView (RectCorner)

- (void)maskToCornerWithRoundingCorners:(UIRectCorner)corner cornerRedius:(CGSize)size;
- (void)setCornerOnTopWithCornerRedius:(CGSize)size;
- (void)setCornerOnBottomWithCornerRedius:(CGSize)size;
- (void)setAllCornerWithCornerRedius:(CGFloat)size;
- (void)setNoneCorner;

@end
#import "UIView+RectCorner.h"

@implementation UIView (RectCorner)

/**
 *  设置View的圆角
 *
 *  @param corner 设置哪一个角为圆角
 *  @param size   圆角的大小
 */
- (void)maskToCornerWithRoundingCorners:(UIRectCorner)corner cornerRedius:(CGSize)size {
    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                     byRoundingCorners:corner
                                           cornerRadii:size];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

/**
 *  设置View上面的圆角(左、右)
 */
- (void)setCornerOnTopWithCornerRedius:(CGSize)size {
    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                     byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
                                           cornerRadii:size];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}
/**
 *  设置View下面的圆角(左、右)
 */
- (void)setCornerOnBottomWithCornerRedius:(CGSize)size {
    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                     byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
                                           cornerRadii:size];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}
/**
 *  设置View四角的圆角
 */
- (void)setAllCornerWithCornerRedius:(CGFloat)size {
    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                          cornerRadius:size];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}
/**
 *  移除圆角
 */
- (void)setNoneCorner{
    self.layer.mask = nil;
}

@end

下面我们看一下怎么调用

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
    imageView.backgroundColor = [UIColor grayColor];
    [imageView maskToCornerWithRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft) cornerRedius:CGSizeMake(10, 10)];
    [self.view addSubview:imageView];
}

运行结果:
这里写图片描述
代码实现很简单,希望能帮到需要的人,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值