iOS设置圆角的几种方式

第一种方法 直接设置layer的属性

这种方法比较简单,代码量较少,但是操作layer肯定会影响性能,会造成离屏渲染。

#pragma mark -  通过设置layer 切圆角
- (void)setLayerCutCirculayWithView:(UIView *) view
{
    view.layer.masksToBounds = YES;
    // 设置圆角半径
    view.layer.cornerRadius = 5.0f; 
}

第二种方法 使用CAShapeLayer和UIBezierPath来设置圆角

这种方法的优点:可以操作任何一个角(左上,右上,左下,右下),并且消耗内存较小,渲染较快。
缺点:操作了layer,对性能有影响,有离屏渲染。

- (void)setLayerAndBezierPathCutCircularWithView:(UIView *) view
{
    // 创建BezierPath 并设置角 和 半径 这里只设置了 左上 和 右上
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];

    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    layer.frame = view.bounds;
    layer.path = path.CGPath;
    view.layer.mask = layer;
}

第三种方法 使用Core Graphics框架 和 UIBezierPath 画出一个圆角

优点:没有操作layer 性能较高。

- (void)setGraphicsCutCirculayWithView:(UIImageView *) view
{       
                                 UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 1.0);
    [[UIBezierPath bezierPathWithRoundedRect:view.bounds cornerRadius:5] addClip];
    [view drawRect:view.bounds];
    view.image = UIGraphicsGetImageFromCurrentImageContext();
    // 结束
    UIGraphicsEndImageContext();
}

github:https://github.com/Perfectmilk/CutCirculayDemo

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值