iOS CGContext实现绘制任意圆角

2023-05-16

少说废话直接上代码

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];

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS CGContext实现绘制任意圆角 的相关文章

随机推荐

  • R-4.0.2 forecast预测

    library 34 forecast 34 skirts lt scan 34 http robjhyndman com tsdldata roberts skirts dat 34 skip 61 5 skirtsts lt ts sk
  • Linux远程连接图形界面的几种方法

    1 xff0c 利用Xmanager xff0c linux启用XDMCP协议 xff08 可直接修改配置文件 xff0c 也可以采用在Xshell中运行gdmconfig或gdmsetup xff0c 选择XDMCP选项卡 xff0c 勾
  • 什么是视图?作用是什么?

    1 1 视图 1 1 1 什么是视图 视图 VIEW 也被称作虚表 xff0c 即虚拟的表 xff0c 是一组数据的逻辑表示 其本质是对应于一条SELECT语句 xff0c 结果集被赋予一个名字 xff0c 即视图名字 视图本身并不包含任何
  • Python元组(tuple)详解

    目录 一 元组的基本概念 xff1a 二 元组的特点 xff1a 1 声明方式 2 访问方式 xff1a 3 元组中元素的修改 xff1a 三 xff0c 对元组的操作 1 元组的切片 xff1a 2 元组的拼接 xff1a 3 元组与数字
  • 解决debian下thinkpad x1没有声音的问题

    提示 文章写完后 目录可以自动生成 如何生成可参考右边的帮助文档 文章目录 问题描述 一 解决方法 二 解决步骤 1 使用non free源 2 更改参数 3 更新源信息 4 安装以下驱动包 5 重启 问题描述 安装完debian 11后
  • windows环境变量配置你知道吗?

    1 什么是windows环境变量 xff1f 我们直接看百度的专业解释 环境变量 xff08 environment variables xff09 一般是指在操作系统中用来指定操作系统运行环境的一些参数 xff0c 如 xff1a 临时文
  • C# 远程RDP共享桌面,控制桌面,远程桌面登录注销,用户切换,包括客户端,服务端软件及代码

    可以一次打开16个桌面 可以支持登录切换用户 包含客户端NoteWin exe WINDOWS服务NoteWinService exe xff0c 当前用户服务NoteWService exe xff0c 远程启动服务AppLoad exe
  • 树莓派3B+ 网页控制GPIO(WebIOPi)

    树莓派3B 43 网页控制GPIO xff08 WebIOPi xff09 对于没接触编程的小白来说 xff0c 让他们编程控制树莓派的GPIO口输出输入 xff0c 那简直就是折磨人 xff0c WebOPi就是一个开源 方便 实用的网页
  • 解决Win10 自动更新问题(可以认为永久关闭更新)

    解决Win10 自动更新问题 xff08 可以认为永久关闭更新 xff09 这是我意外想到的一个方法 xff0c 步骤十分简单 xff0c 什么都不用设置 xff0c 不过每隔35天就要操作一下 xff0c 因为win10最多临时暂停35天
  • * failed to start daemon *解决方法

    16 11 23 error could not install smartsocket listener cannot bind to 127 0 0 1 5037 通常每个套接字地址 协议 网络地址 端口 只允许使用一次 10048 1
  • 【工具】验证是否安装cuda和cuDNN

    验证是否安装cuda 验证cuda是否安装方法1 span class token keyword import span torch span class token keyword print span span class token
  • 常用的url链接

    maven库 xff1a https mvnrepository com 一些基本工具的安装和语法 xff0c 如mysql docker https www runoob com docker centos docker install
  • 达梦7数据库后台导入导出命令解释

    达梦7使用的dexp和dimp xff0c 用法和exp和imp类似 xff0c 在命令后直接加上help可以查看帮助 吐槽一下竟然没有 dexp dimp help或 xff1f 这种操作 xff0c 必须是help才可以识别 格式 de
  • 麒麟V10修改root密码

    不知为何 xff0c root密码登录不上去 xff0c 几番测试都说是密码错误 基本环境 xff1a 国产服务器和麒麟V10 人不在机房 步骤如下 xff1a 1 连接服务器 xff0c 打开带外界面 2 打开控制台界面 3 重启电脑 x
  • python 图片文字提取

    转载 xff1a python怎么读取png python 图像处理 天道酬勤 花开半夏 图像处理不是一件简单的工作 对你来说 xff0c 一看到某样东西 xff0c 马上就知道你在看什么很容易 但是 xff0c 电脑不是这样工作的 对你来
  • C++ MFC调用JS代码获取返回值

    C 43 43 有时候会需要调用JS代码 xff0c 这对于C 43 43 来说或者对于国内来说一直是比较蛋疼的问题 xff0c 主要是资料少 xff0c 微软提供了一个COM组件 xff0c 里面包含有JS引擎 xff0c 这个组件就是
  • Python类基本概念 类对象、类属性、实例化、实例对象、实例属性、类方法、类构造函数、类继承

    想看视频的直接移步 python类对象类属性类的实例化实例对象实例属性 哔哩哔哩 bilibili python类方法类构造函数实例方法静态方法 哔哩哔哩 bilibili python类的继承代码实现 哔哩哔哩 bilibili 类对象
  • 用API获取IP地址

    设计蜂巢IP地址查询接口 xff1a http www hujuntao com api ip ip php腾讯IP地址查询接口 xff1a http fw qq com ipaddress新浪IP地址查询接口 xff1a http int
  • 打印出所有的水仙花数 Java实现

    题目 xff1a 打印出所有的水仙花数 所谓水仙花数是指一个三位数 xff0c 其各位数字立方和等于该数本身 例如 xff1a 153是一个 水仙花数 xff0c 因为153 61 1的三次方 xff0b 5的三次方 xff0b 3的三次方
  • iOS CGContext实现绘制任意圆角

    少说废话直接上代码 CGSize size 61 view bounds size UIGraphicsBeginImageContextWithOptions size NO UIScreen mainScreen scale CGCon