ios view 切上部分圆角_IOS开发入门之给view添加圆角并指定位置

本文介绍了在iOS开发中如何为UIView添加圆角并指定位置,包括使用图层属性、贝塞尔曲线以及CAShapeLayer的方法,并推荐了性能更优的第三种方案。
摘要由CSDN通过智能技术生成

本文将带你了解IOS开发入门之给view添加圆角并指定位置,希望本文对大家学IOS有所帮助。

ios中给view添加圆角并指定位置

在ios开发中,为了有个不错的UI交互效果,我们经常会用到为视图添加圆角,或者指定某个位置去切割圆角。

* 简单实现*具体有三种方式:

第一种:设置图层的属性

使用简单,性能不好 在开发中我们应该减少使用

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

//只需要设置layer层的两个属性

//设置圆角

imageView.layer.cornerRadius = imageView.frame.size.width / 2;

//将多余的部分切掉

imageView.layer.masksToBounds = YES;

[self.view addSubview:imageView];

2、第二种使用贝塞尔曲线UIBezierPath,开启图形上下文画出一个圆

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

imageView.image = [UIImage imageNamed:@"1"];

//开始对imageView进行画图

UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, [UIScreen mainScreen].scale);

//使用贝塞尔曲线画出一个圆形图

[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];

[imageView drawRect:imageView.bounds];//画图

imageView.image = UIGraphicsGetImageFromCurrentImageContext();

//结束画图--这个时候的image是圆形的

UIGraphicsEndImageContext();//结束

[self.view addSubview:imageView];

3、第三种使用UIBezierPath和CAShareLayer设置圆角

UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(10, 100, 300, 400)];

view1.backgroundColor = [UIColor redColor];

[self.view addSubview:view1];

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view1.bounds byRoundingCorners:UIRectCornerTopRight| UIRectCornerTopLeft cornerRadii:CGSizeMake(10, 10)];//指定圆角位置 大小

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

masklayer.frame = view1.bounds;

masklayer.path = path.CGPath;

view1.layer.mask = masklayer;

比较推荐使用第三种,内存消耗少,速度快。

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值