iOS 设置View任意角为圆角

通过代码设置UIView的任意一个角为圆角

先创建RectCorner 继承自UIView  

.h代码

#import <UIKit/UIKit.h>

 

@interface RectCorner : UIView

/**

 * 顶部圆角

 */

- (void)setCornerOnTop;

/**

 *  底部圆角

 */

- (void)setCornerOnBottom;

/**

 *  全部圆角

 */

- (void)setAllCorner;

@end

 

.m代码

#import "RectCorner.h"

 

@implementation RectCorner

 

- (void)setCornerOnTop {

    UIBezierPath *maskPath;

    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds

                                     byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)

                                           cornerRadii:CGSizeMake(8.0, 8.0)];

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];

    maskLayer.frame = self.bounds;

    maskLayer.path = maskPath.CGPath;

    self.layer.mask = maskLayer;

}

- (void)setCornerOnBottom {

    UIBezierPath *maskPath;

    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds

                                     byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)

                                           cornerRadii:CGSizeMake(8.0, 8.0)];

    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:8.0];

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];

    maskLayer.frame = self.bounds;

    maskLayer.path = maskPath.CGPath;

    self.layer.mask = maskLayer;

    

}

- (void)setNoneCorner {

    self.layer.mask = nil;

}

@end

 

使用方法

#import "ViewController.h"

#import "RectCorner.h"

 

@interface ViewController ()

@property (nonatomic,strong)RectCorner *rc;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    

    [super viewDidLoad];

    //四角全不为圆角

    RectCorner *rc = [[RectCorner alloc]init];

    rc.backgroundColor = [UIColor magentaColor];

    rc.frame = CGRectMake(100, 100, 100, 100);

    [self.view addSubview:rc];

    [rc setAllCorner];

  

    

    //底部两个角为圆角

    RectCorner *rc1 = [[RectCorner alloc]init];

    rc1.backgroundColor = [UIColor magentaColor];

    rc1.frame = CGRectMake(100, 220, 100, 100);

    [self.view addSubview:rc1];

    [rc1 setCornerOnBottom];

 

    //顶部两个角为圆角

    RectCorner *rc2 = [[RectCorner alloc]init];

    rc2.backgroundColor = [UIColor magentaColor];

    rc2.frame = CGRectMake(100, 350, 100, 100);

    [self.view addSubview:rc2];

    [rc2 setCornerOnBottom];

    

    

    //任意角为圆角

    UIView *bgView = [[UIView alloc]init];

    bgView.frame = CGRectMake(220, 200, 50, 50);

    bgView.backgroundColor = [UIColor purpleColor];

    bgView.userInteractionEnabled = YES;

    [self.view addSubview:bgView];

    

    /**

     UIRectCornerTopLeft 上左

     UIRectCornerTopRight上右

     UIRectCornerBottomRight下右

     UIRectCornerBottomRight下右

     */

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bgView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];

    maskLayer.frame = bgView.bounds;

    maskLayer.path = maskPath.CGPath;

    bgView.layer.mask = maskLayer;

    

}

 

@end

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值