ios中为视图添加圆角
1.使用
layer设置指定圆角 或者设定一个或几个圆角
由于项目中需要给按钮左下 和左上加圆角,我司可爱的ui君并不想给我切图。
所以只有自己画了。由于很久没有用过这些知识,花了一些时间,
故记入笔记。
也可以用来画半圆。
选择要画圆角的位置只需要改变枚举即可。
有空能封装一下更好。

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.statusLabel.bounds
byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft
cornerRadii:CGSizeMake(self.statusLabel.height/2, self.statusLabel.height/2)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.statusLabel.bounds;
maskLayer.path = maskPath.CGPath;
self.statusLabel.layer.mask = maskLayer;
//////
UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake((bgView.width-125)/2, -10, 125, 10)];
whiteView.backgroundColor = [UIColor whiteColor];
if (@available(iOS 11.0, *)) {
whiteView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
whiteview.layer.maskstoBound = true;
whiteview.layer.coneradio = 5.0;
whiteview.layer.shouldRasterize = YES; //圆角缓存
whiteview.layer.rasterizationScale = [UIScreen mainScreen].scale;//提高流畅度
经测试 加上后两句之后流畅度有了提高。
} else {
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:whiteView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(5, 5)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
//设置大小
maskLayer.frame = whiteView.bounds;
//设置图形样子
maskLayer.path = maskPath.CGPath;
whiteView.layer.mask = maskLayer;
}
iOS设置圆角的方法及指定圆角的位置
- 在iOS开发中,我们经常会遇到设置圆角的问题, 以下是几种设置圆角的方法:
第一种方法: 通过设置layer的属性
代码:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"willwang"]];
//只需要设置layer层的两个属性
//设置圆角
imageView.layer.cornerRadius =50
//将多余的部分切掉
imageView.layer.masksToBounds = YES;
imageView.layer.shouldRasterize = YES; //圆角缓存
imageView.layer.rasterizationScale = [UIScreen mainScreen].scale;//提高流畅度
经测试 加上后两句之后流畅度有了提高。
[self.view addSubview:imageView];
- 这个实现方法里maskToBounds会触发离屏渲染(offscreen rendering),GPU在当前屏幕缓冲区外新开辟一个渲染缓冲区进行工作,也就是离屏渲染,这会给我们带来额外的性能损耗,如果这样的圆角操作达到一定数量,会触发缓冲区的频繁合并和上下文的的频繁切换,性能的代价会宏观地表现在用户体验上<掉帧>不建议使用.
第二种方法: 使用贝塞尔曲线UIBezierPath和Core Graphics框架画出一个圆角
效果图:
代码:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageNamed:@"willwang"];
//开始对imageView进行画图
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 0.0);
//使用贝塞尔曲线画出一个圆形图
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];
[imageView drawRect:imageView.bounds];
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
//结束画图
UIGraphicsEndImageContext();
[self.view addSubview:imageView];
- UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) 各参数含义:
- size —— 新创建的位图上下文的大小
- opaque —— 透明开关,如果图形完全不用透明,设置为YES以优化位图的存储。
- scale —— 缩放因子 iPhone 4是2.0,其他是1.0。虽然这里可以用[UIScreen mainScreen].scale来获取,但实际上设为0后,系统就会自动设置正确的比例
第三种方法: 使用Core Graphics框架画出一个圆角
代码:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 100, 100)];
imageView.image = [UIImage imageNamed:@"willwang"];
//开始对imageView进行画图
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
// 获得图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 设置一个范围
CGRect rect = CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height);
// 根据一个rect创建一个椭圆
CGContextAddEllipseInRect(ctx, rect);
// 裁剪
CGContextClip(ctx);
// 将原照片画到图形上下文
[imageView.image drawInRect:rect];
// 从上下文上获取剪裁后的照片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// 关闭上下文
UIGraphicsEndImageContext();
imageView.image = newImage;
[self.view addSubview:imageView];
第四种方法: 使用CAShapeLayer和UIBezierPath设置圆角
代码:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageNamed:@"willwang"];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageView.bounds.size];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
//设置大小
maskLayer.frame = imageView.bounds;
//设置图形样子
maskLayer.path = maskPath.CGPath;
imageView.layer.mask = maskLayer;
[self.view addSubview:imageView];
第四种方法延伸 指定需要成为圆角的角
方法:
+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect
byRoundingCorners:(UIRectCorner)corners
cornerRadii:(CGSize)cornerRadii
corners参数指定了要成为圆角的角, 枚举类型如下:
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};
效果图:
代码:
//设置视图位置和大小
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(120, 300, 100, 50)];
//设置背景颜色
myView.backgroundColor = [UIColor redColor];
//添加
[self.view addSubview:myView];
//绘制圆角 要设置的圆角 使用“|”来组合
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:myView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
//设置大小
maskLayer.frame = myView.bounds;
//设置图形样子
maskLayer.path = maskPath.CGPath;
myView.layer.mask = maskLayer;
UILabel *label = [[UILabel alloc]init];
//添加文字
label.text = @"willwang";
//文字颜色
label.textColor = [UIColor whiteColor];
[myView addSubview: label];
//自动布局
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(myView);
}];
第五种方法 在storyboard或xib中设置
/////////////
第一种方法:通过设置layer的属性
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];
注:禁用。。影响性能。
第二种方法:使用贝塞尔曲线UIBezierPath和Core Graphics框架画出一个圆角
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageNamed:@"1"];
//开始对imageView进行画图
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
//使用贝塞尔曲线画出一个圆形图
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];
[imageView drawRect:imageView.bounds];
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
//结束画图
UIGraphicsEndImageContext();
[self.view addSubview:imageView];
第三种方法:使用CAShapeLayer和UIBezierPath设置圆角
首先需要导入<AVFoundation/AVFoundation.h>
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageNamed:@"1"];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageView.bounds.size];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
//设置大小
maskLayer.frame = imageView.bounds;
//设置图形样子
maskLayer.path = maskPath.CGPath;
imageView.layer.mask = maskLayer;
[self.view addSubview:imageView];
}
---------------------
作者:weixin_34381687
来源:CSDN
原文:https://blog.csdn.net/weixin_34381687/article/details/87515690
版权声明:本文为博主原创文章,转载请附上博文链接!
ios中为视图添加圆角的更多相关文章
- 浅谈iOS中的视图优化
引言: 让我们来思考几个问题,你开发过的产品,它还有可以优化的地方吗?能增加它的帧率吗?能减少多余的CPU计算吗?是不是存在多余的GPU渲染?业务这点工作量对于越来越强大的设备面前显得微不足道,但作为 ...
- iOS中为网站添加图标到主屏幕
1 <link rel="apple-touch-icon-precomposed" href="icon.png"/> 2 <link re ...
- Swift - iOS中各种视图控制器(View Controller)的介绍
在iOS中,不同的视图控制器负责不同的功能,采用不同的风格向用户呈现信息.下面对各个视图控制器做个总结: 1,标准视图控制器 - View Controller 这个控制器只是用来呈现内容.通常会用来 ...
- [Xcode 实际操作]二、视图与手势-(5)给图像视图添加圆角效果
目录:[Swift]Xcode实际操作 本文将演示给矩形图片添加圆角效果 import UIKit class ViewController: UIViewController { override ...
- IOS之表视图添加搜索栏
下面是我们要实现的效果.本效果是在上一篇自定义表视图的基础上进行更改的. 1.将Search bar and search display拖动到ViewController中.不要添加Sear ...
- iOS中为网站添加图标到主屏幕以及增加启动画面
虽然没有能力开发Native App,但还是可以利用iOS中Safari浏览器的特性小小的折腾一下,做一个伪Web App满足下小小的虚荣心的. 既然是在iOS中的Safari折腾的,那么代码中利用到 ...
- iOS中创建自定义的圆角按钮
iOS中很多时候都需要用到指定风格的圆角按钮,尽管UIButton提供了一个方式创建圆角按钮: + (id)buttonWithType:(UIButtonType)buttonType;//指定bu ...
- iOS中通知的添加和移除
我们都知道viewWillAppear:方法是在控制器的view将要显示的时候调用的,而viewWillDisappear:方法是在控制器的view将要隐藏的时候调用.很多时候我们根据自身需要将相关代 ...
- iOS 中 为UIView添加背景图片
创建UIImage的方法有两种: UIImage *image = [UIImageimageNamed:@"image.jpg"];//这种不释放内存,要缓存 NSString ...
随机推荐
- pikachu平台搭建
1.将pikachu转移至htdocs 2.然后打开pikachu文件夹里的inc文件夹 3.里面对应的内容该成之前刚刚设置好的数据库服务器地址,用户名,密码和端口号 4.打开浏览器,输入http:/ ...
- the MTS failed last time时的解决办法
关于6.6.3SP2版本提示The MTS failed last time 1.1 发生前提条件 在重启系统 shutdown -r now后,网页打不开,发现MTS服务无法启动,我自己涉及的 ...
- drf解析模块,异常模块,响应模块,序列化模块
复习 """ 1.接口:url+请求参数+响应参数 Postman发送接口请求的工具 method: GET url: https://api.map.baidu.com ...
- 040_字符串连接符 041_条件运算符目 042_运算符优先级_逻辑与或优先问题 043_自动类型转化 044_强制类型转换 045_基本类型常见错误_溢出_L问题
040_字符串连接符 package test_package; /** * 字符串运算符 * @author * */public class TestOperator05 { public sta ...
- 【资源分享】Half-Life(半条命)中英版
*----------------------------------------------[下载区]----------------------------------------------* ...
- [转]使用HttpOnly提升Cookie安全性
原文:https://www.cnblogs.com/zlhff/p/5477943.html 在介绍HttpOnly之前,我想跟大家聊聊Cookie及XSS. 随着B/S的普及,我们平时上网都是依赖 ...
- bootstrap的pillbox使用
使用bootstrap的cameo模版,搭建了一个cms系统,使用pillbox做显示的时候,出现点击×失败的问题. 分析了一下pillbox这个控件的使用方法. pillbox的样例在cameo/f ...
- 记录STM32调试
问题:加入红外初始化后,程序卡在红外初始化处 解决思路: 1.确认时钟是不是好的 2.把定时器分解调试(输入捕获.溢出分开一步一步弄) 已解决:定时器的溢出中断 注意:STM32Cube配置好后,需要 ...
- PHP转换oracle数据库的date类型
今天圣诞节啊,圣诞节快乐啊! 最近遇到一个很纠结的事,就是我在plsql里面查的是这样的,很正常, 但是我用程序查出来就是这样的,啊啊啊,真是崩溃啊 但是我传数据需要上面那种格式,而且我对oracle ...
- 201771010135 杨蓉庆/张燕《面对对象程序设计(java)》第十三周学习总结
1.实验目的与要求 (1) 掌握事件处理的基本原理,理解其用途: (2) 掌握AWT事件模型的工作机制: (3) 掌握事件处理的基本编程模型: (4) 了解GUI界面组件观感设置方法: (5) 掌握W ...