本文将带你了解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频道!