iOS CGContext实现绘制任意圆角
少说废话直接上代码
CGSize size = view.bounds.size;
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(contextRef, 1);
CGContextSetStrokeColorWithColor(contextRef, [UIColor clearColor].CGColor);
CGContextSetFillColorWithColor(contextRef, view.backgroundColor.CGColor);
view.backgroundColor = [UIColor clearColor];
CGFloat halfBorderWidth = 1 / 2.0;
CGFloat width = size.width;
CGFloat height = size.height;
CGContextMoveToPoint(contextRef, width - halfBorderWidth, radius + halfBorderWidth);
if (corner == UIRectCornerAllCorners) {
CGContextAddArcToPoint(contextRef, width - halfBorderWidth, height - halfBorderWidth, width - radius - halfBorderWidth, height - halfBorderWidth, radius); // 右下角角度
CGContextAddArcToPoint(contextRef, halfBorderWidth, height - halfBorderWidth, halfBorderWidth, height - radius - halfBorderWidth, radius); // 左下角角度
CGContextAddArcToPoint(contextRef, halfBorderWidth, halfBorderWidth, width - halfBorderWidth, halfBorderWidth, radius); // 左上角
CGContextAddArcToPoint(contextRef, width - halfBorderWidth, halfBorderWidth, width - halfBorderWidth, radius + halfBorderWidth, radius); // 右上角
}
else {
if (corner & UIRectCornerBottomRight) {
CGContextAddArcToPoint(contextRef, width - halfBorderWidth, height - halfBorderWidth, width - radius - halfBorderWidth, height - halfBorderWidth, radius); // 右下角角度
CGContextMoveToPoint(contextRef, width - radius - halfBorderWidth, height - halfBorderWidth);
}
else {
CGContextAddLineToPoint(contextRef, width - halfBorderWidth, height - halfBorderWidth); // 右下角角度
}
if (corner & UIRectCornerBottomLeft) {
CGContextAddArcToPoint(contextRef, halfBorderWidth, height - halfBorderWidth, halfBorderWidth, height - radius - halfBorderWidth, radius); // 左下角角度
CGContextMoveToPoint(contextRef, halfBorderWidth, height - radius - halfBorderWidth);
}
else {
CGContextAddLineToPoint(contextRef, halfBorderWidth, height - halfBorderWidth); // 左下角角度
}
if (corner & UIRectCornerTopLeft) {
CGContextAddArcToPoint(contextRef, halfBorderWidth, halfBorderWidth, width - halfBorderWidth, halfBorderWidth, radius); // 左上角
CGContextAddLineToPoint(contextRef, width - halfBorderWidth, halfBorderWidth);
}
else {
CGContextAddLineToPoint(contextRef, halfBorderWidth, halfBorderWidth); // 左上角
}
if (corner & UIRectCornerTopRight) {
CGContextAddArcToPoint(contextRef, width - halfBorderWidth, halfBorderWidth, width - halfBorderWidth, radius + halfBorderWidth, radius); // 右上角
CGContextAddLineToPoint(contextRef, width - halfBorderWidth, radius + halfBorderWidth);
}
else {
CGContextAddLineToPoint(contextRef, width - halfBorderWidth, halfBorderWidth); // 右上角
}
CGContextAddLineToPoint(contextRef, width - halfBorderWidth, radius + halfBorderWidth);
}
CGContextDrawPath(contextRef, kCGPathFillStroke);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[view viewWithTag:1999999] removeFromSuperview];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = view.frame;
imageView.tag = 1999999;
[view insertSubview:imageView atIndex:0];
iOS CGContext实现绘制任意圆角相关推荐
- iOS UIView异步绘制实现圆角的方案
前言 关于离屏渲染.圆角卡顿,网络上已经有大量理论性的文章.如果有兴趣推荐看YYKit作者的相关文章. 包括我在内,大部分人看了理论文章后,可能还是一知半解.想要看具体实现,能找到的都是YYKit.A ...
- 不使用羊角曲线绘制G2圆角
知识补充 G0连续是点连续 G1连续是斜率连续 G2连续是曲率连续 用羊角螺线绘制G2圆角 首先绘制出一个羊角螺线 羊角螺线无法绘制一个关于y=x对称的直角过渡,但可以绘制0°-45°的过渡. 所以可 ...
- QML Shape实现任意圆角Rectangle矩形
前言 QML中有时候需要实现一些特殊定制化页面,比如不同的圆角矩形,如果直接用Rectangle控件定义radius圆角属性,会将四个角统一设置成一样的圆角,但是如果仅仅只想实现两个圆角效果,另外两个 ...
- DDGScreenShot —iOS 图片裁剪,切圆角,加边框,你还用cornerRadius,还有更高级的方法...
写在前面 我们肯定做过这样的需求,给一个图片切圆角, 当然我们大多采用简单粗暴的方法 myIcon.layer.cornerRadius = 16.5 myIcon.layer.masksToBoun ...
- 地图上绘制任意角度的椭圆_地图上的总椭圆
地图上绘制任意角度的椭圆 或者,如何选择下班后去海滩的最佳方式 (Or, how to choose the best way to walk to the beach after work) It ...
- iOS开发小技巧 -- tableView-section圆角边框解决方案
iOS开发小技巧 -- tableView-section圆角边框解决方案 参考文章: (1)iOS开发小技巧 -- tableView-section圆角边框解决方案 (2)https://www. ...
- ios view 切上部分圆角_IOS开发入门之给view添加圆角并指定位置
本文将带你了解IOS开发入门之给view添加圆角并指定位置,希望本文对大家学IOS有所帮助. ios中给view添加圆角并指定位置 在ios开发中,为了有个不错的UI交互效果,我们经常会用到为视图添加 ...
- 定义由4条线和4个圆弧组成的Box类,绘制一个圆角矩形(可填充)。
最终效果: //定义由4条线和4个圆弧组成的Box类,绘制一个圆角矩形. #include"../实践/GUI/Simple_window.h" #include"GU ...
- matlab 绘制任意方向和位置的空间圆锥体
matlab 绘制任意方向和位置的空间圆锥体 function cone3(X1,X2,r,color) %一个简单的例子:cone3([1 2 3],[7 8 9],1,'b');%两个空间点位置, ...
最新文章
- 把canvas放在盒子内_如何将您的专业知识放在盒子中并出售
- Mtop,Mysql Top,一个Mysql的监控工具
- electron创建菜单
- python批量新建文件夹_python批量创建指定名称的文件夹
- 深入Lock锁底层原理实现,手写一个可重入锁
- PHP 获取两个日期之间所有日期
- C语言关系运算符介绍和示例
- spring与mybatis
- Farthest sampling on 3d mesh with mesh kept
- 三面网易,四面阿里,五年开发经验程序员剑指大厂,稳拿offer
- ArcGIS地理配准(Georeferencing)详解
- python绘图库seaborn_Python绘图库:Seaborn 介绍
- CMD No compiler is provided in this environment. Perhaps you are running on
- 西部世界:币本位是什么?
- LeetCode-Python-274. H指数
- python 标签_Python DataFrame中 打标签/归类 的6种方法
- 未能解析主引用“Fiddler”,因为它是针对“.NETFramework,Version=v4.6.1”框架生成的。该框架版本高于当前目标框架“.NETFramework,Version=v4.5
- 微信多媒体文件speex格式转为mp3文件格式
- 使用U盘为台式机安装centos7
- python全国计算机等级考试_Python二级教程(全国计算机等级考试)
热门文章
- 数据库之SQL(INSERT,INSERT SELECT,INSERT INTO语句)
- microbit python中文_microbit之mpython的API
- SAP:MIGO校验自定义字段增强
- 【机器学习线性代数】19 复数域中的向量与矩阵
- Widget中的一些基本概念
- FGH40N60SMD安森美车规IGBT,ASEMI原厂代理FGH40N60SMD
- 【AI达人创造营第二期】基于PaddleClas的新冠肺炎CT影像的分类
- 小程序开发教程,深入解析android核心组件和应用框架,附面试题答案
- 华硕编程竞赛11月JAVA专场 G题飞行棋 题解
- Java系列之:var关键字