ios - 带有变换的 CGPathCreateWithEllipseInRect

标签 ios swift cgaffinetransform cgpath

在 Swift 中尝试使用 CGPathCreateWithEllipseInRect 函数,我遇到了这个问题:

这段代码如我所料地工作,我得到了一条路径并可以使用它:

CGPathCreateWithEllipseInRect(CGRect(x: xCoord, y: yCoord,
                width: theWidth, height: theHeight), nil)

但是这个不行:

var affineTransform = CGAffineTransformMakeRotation(1.0)

CGPathCreateWithEllipseInRect(CGRect(x: xCoord, y: yCoord,
                width: theWidth, height: theHeight), &affineTransform)

似乎我根本找不到路径(或一条空路径)。我做错了什么?

最佳答案

你的第二个代码

var affineTransform = CGAffineTransformMakeRotation(1.0)

let path = CGPathCreateWithEllipseInRect(CGRect(x: xCoord, y: yCoord,
                width: theWidth, height: theHeight), &affineTransform)

是正确的并且确实有效。但是请注意,您创建了一个旋转 围绕 View 原点的角度为 1.0 * 180/π ≈ 57 度 (默认情况下是左上角)。 这可能会将椭圆移出 View 的可见边界。

传递一个 nil 转换的等价物是一个旋转 关于零角度

var affineTransform = CGAffineTransformMakeRotation(0.0)

如果您的意图是旋转大约 1 度,则使用

var affineTransform = CGAffineTransformMakeRotation(CGFloat(1.0 * M_PI/180.0))

如果您打算围绕其中心旋转椭圆, 那么你必须将旋转与翻译结合起来 使椭圆的中心成为坐标系的原点:

var affineTransform = CGAffineTransformMakeTranslation(xCoord + theWidth/2.0, yCoord + theHeight/2.0)
affineTransform = CGAffineTransformRotate(affineTransform, angle)
let path = CGPathCreateWithEllipseInRect(CGRect(x: -theWidth/2.0, y: -theHeight/2.0,
    width: theWidth, height: theHeight), &affineTransform)

关于ios - 带有变换的 CGPathCreateWithEllipseInRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34584520/

相关文章:

已经呈现的 iOS 8 Xcode 6.1 Swift prepareForSegue Controller (空)

ios - UIimagejperepresentation 压缩质量与其他算法的比较

ios - 将转换应用到 UITextView - 防止内容调整大小

iphone - CGAffineTransform 和缩放到图像的中心

swift - Swift 中的视频合并方向

ios - 修复 ARC 中潜在的内存泄漏

ios - 选项卡索引在 App Delegate 类中正确,但在 App Delegate 对象中不正确

ios - 返回 iOS UILabel 方法的正确方法

swift - 如何使 SwiftUI 按钮固定大小?

ios - JSON 解析成功后本应存储 JSON 的数组为空