swift - 为什么cornerRadius的最大值不是矩形的一半?用 UIBezierPath 快速绘制roundedRect
问题描述
正如文件所说,cornerRadius 不能大于一半宽度,但在我的代码中,圆形矩形只有宽度的 1/3 变成了圆形,不知道为什么?需要帮忙
文档: https ://developer.apple.com/documentation/uikit/uibezierpath/1624356-init
cornerRadius:每个角椭圆的半径。值为 0 会生成一个没有圆角的矩形。大于矩形宽度或高度一半的值被适当地限制为宽度或高度的一半。
var path = UIBezierPath(roundedRect: CGRect(x: 50, y: 200, width: 94, height: 94), cornerRadius: 31)
path.stroke()
path = UIBezierPath(roundedRect: CGRect(x: 50, y: 300, width: 94, height: 94), cornerRadius: 30.5)
path.stroke()
path = UIBezierPath(roundedRect: CGRect(x: 50, y: 400, width: 94, height: 94), cornerRadius: 31.5)
path.stroke()
解决方案
我认为这是因为类型转换错误。
尝试这个。
let xCor: CGFloat = 50.0
let width: CGFloat = 94.0
let height: CGFloat = 94.0
let cornerRadius: CGFloat = width / 3.0
var path = UIBezierPath(roundedRect: CGRect(x: xCor, y: 200.0, width: width, height: height), cornerRadius: cornerRadius)
path.stroke()
path = UIBezierPath(roundedRect: CGRect(x: xCor, y: 300.0, width: width, height: height), cornerRadius: cornerRadius)
path.stroke()
path = UIBezierPath(roundedRect: CGRect(x: xCor, y: 400.0, width: width, height: height), cornerRadius: cornerRadius)
path.stroke()
PS:没有通过运行此代码检查让我知道这里是否有任何问题。
推荐阅读
- python - 如何将字典元素相乘并得到一个总和
- javascript - 如果在 rxjs 中没有按顺序重复 3 次,如何跳过 0 值
- pyspark - 从 PySpark 中的数组中提取第一个非 Null 正元素
- java - Caffeine Expiry 中如何设置多个过期标准?
- r - 如何使用 EBImage 将灰度图像列表转换为 RGB 图像
- javascript - 键入传入函数
- python - TypeError:'numpy.int64'对象不可调用,每当我在python中调用len函数时?
- c++ - 2d char Array 打印方块。我如何解决它?
- swiftui - 在 SwiftUI 中更改 SpriteKit 场景
- python - 将值字符串传递到 Python 的 mySQL 连接器时遇到问题