iOS 设置圆角

第一种方式:通过设置控件的layer属性

该方法是iOS实现圆角的方法中最简单的一种,比较影响性能

实现代码如下所示:

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
//    设置圆角
    imageView.layer.cornerRadius = imageView.frame.size.width/2;
//    将多余的部分剪切掉
    imageView.layer.masksToBounds = YES;
    [self.view addSubview:imageView];
如果要给控件设置边框可以设置layer的以下两个属性

//    设置边框 边框的宽度为10
    imageView.layer.borderWidth = 10;
//    设置边框的颜色  红色
    imageView.layer.borderColor = [UIColor redColor].CGColor;

第二种方式:使用贝塞尔曲线UIBezierPath和Core Graphics框架画出一个圆角

实现代码如下所示:

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    imageView.image =[ UIImage imageNamed:@"logo1"];
    //    开始对imageView进行画圆
    UIGraphicsBeginImageContextWithOptions(imageView.frame.size, NO, 1.0);
    //    使用贝塞尔曲线画出一个圆形图
    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.bounds.size.width] addClip];
    [imageView drawRect:imageView.bounds];
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    //结束画图
    UIGraphicsEndImageContext();
    [self.view addSubview:imageView];
第三种方式:使用 CAShapeLayer和贝塞尔曲线UIBezierPath

实现代码如下所示:

    UIImageView *Image = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    Image.image = [UIImage imageNamed:@"logo1"];
    UIBezierPath *MaskPath = [UIBezierPath bezierPathWithRoundedRect:Image.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:Image.bounds.size];
    CAShapeLayer *MaskLayer = [[CAShapeLayer alloc]init];
//    这是大小
    MaskLayer.frame = Image.bounds;
//    设置图形的样子
    MaskLayer.path = MaskPath.CGPath;
    Image.layer.mask = MaskLayer;
    [self.view addSubview:Image];

注意:第三种实现方式在使用的时候要导入AVFoundation.framework,然后导入头文件

#import <AVFoundation/AVFoundation.h>

以上三种实现圆角的方式中,第三种方式对内存的消耗最少。


在xib中对实现控件的圆角设置

第一步:选择要设置为圆角的控件。操作如下图所示:


第二步:添加KeyPath


设置完成之后如下图所示:

控件的KeyPath设置完成之后并不会立刻在Xib中显示为圆角,只有当程序运行的时候才会显示出圆角


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值