gpt4 book ai didi

ios - 带有变换的 CGPathCreateWithEllipseInRect

转载 作者:行者123 更新时间:2023-11-28 06:52:11 24 4
gpt4 key购买 nike

在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com