iOS 让UIView 和 UIButton的左上角和右上角为圆角

                      联系人:石虎 QQ:1224614774  昵称: 嗡嘛呢叭咪哄

                               QQ群:807236138  群称: iOS 技术交流学习群

                              QQ群:713799633   群称:iOS技术交流学习群-2

一、概念

/**

UIView 的左上角和右上角为圆角

UIButton左上角和右上角圆角

注意:直接拷贝方法直接可以使用

简书:https://www.jianshu.com/p/f2482a9f45c2

*/

 

二、UIView 的左上角和右上角为圆角

效果图:

UIView 的左上角和右上角为圆角

声明:

@property(nonatomic,strong) UIView *platFormRadiusView;

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.title = @"左上和右上为圆角";

    [self platFormRadiusView];

}

实现:

-(UIView *)platFormRadiusView{

    if (!_platFormRadiusView) {

        _platFormRadiusView = [[UIView alloc] init];

        _platFormRadiusView.backgroundColor = [UIColor redColor];

        _platFormRadiusView.frame = CGRectMake(0, 100,self.view.frame.size.width , 200);

        [self.view addSubview:_platFormRadiusView];

 

        // 左上和右上为圆角

        UIBezierPath *cornerRadiusPath = [UIBezierPath bezierPathWithRoundedRect:_platFormRadiusView.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(15, 15)];

        

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

        cornerRadiusLayer.frame = _platFormRadiusView.bounds;

        cornerRadiusLayer.path = cornerRadiusPath.CGPath; _platFormRadiusView.layer.mask = cornerRadiusLayer;

    }

    return _platFormRadiusView;

}

 

三、UIButton 的左上角和右上角为圆角

效果图:

UIButton 的左上角和右上角为圆角

- (void)viewDidLoad {

    [super viewDidLoad];

    self.title = @"Button左上和右上为圆角";

    [self platFormRadiusButton];

}

实现:

-(void)platFormRadiusButton{

    UIButton *shButton = [UIButton buttonWithType:UIButtonTypeCustom];

    shButton.frame = CGRectMake(0, 100, self.view.frame.size.width, 200);

    shButton.backgroundColor = [UIColor redColor];

    [shButton setTitle:@"石虎测试Button" forState:0];

    shButton.tag = 1;

    [self.view addSubview:shButton];

    //设置圆角

 UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:shButton.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(15, 15)];

    

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

    maskLayer.frame = shButton.bounds;

    maskLayer.path = maskPath.CGPath;

    shButton.layer.mask = maskLayer;

}

 

谢谢!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值