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