Ios CGPathCreateWithEllipseInRect与变换
在Swift中尝试使用Ios CGPathCreateWithEllipseInRect与变换,ios,swift,cgaffinetransform,cgpath,Ios,Swift,Cgaffinetransform,Cgpath,在Swift中尝试使用CGPathCreateWithEllipseInRect函数时,我遇到了以下问题: 这段代码按照我的预期工作,我得到了一条路径并可以利用它: CGPathCreateWithEllipseInRect(CGRect(x: xCoord, y: yCoord, width: theWidth, height: theHeight), nil) 但这一条不起作用: var affineTransform = CGAffineTransform
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)
是正确的,确实有效。但是请注意,您创建了一个旋转
角度为1.0*180/π≈ 围绕视图原点旋转57度
(默认情况下为左上角)。
这可能会将椭圆移出视图的可见边界
传递nil
变换的等效方法是旋转
大约零度角
var affineTransform = CGAffineTransformMakeRotation(0.0)
如果你的目的是旋转一度,那么使用
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)
你是对的,问题(我没想到)比我想象的要简单。我只是没有绕着正确的点旋转。我想让我绕椭圆中心旋转。@Michel:你现在可能已经知道了。如果没有:请参阅更新的答案。是的,谢谢。事实上,我使用了相同的方法,加上结尾处与第一个相反的翻译,以恢复我原来的位置。但是看到你上一次的指导,你似乎在用一种稍微不同的方式做同样的事情。我没有改变我的上一条指令,只是在仿射变换上工作。