如何使用 UIBezierPath 在 Swift using/without 中循环图像?

How do I circulate an image in Swift using/without using UIBezierPath?

我正在尝试使用 UIBezierPath 制作一张图片。这是我用来确定图像的 xy 坐标、宽度和高度的代码。

var frm : CGRect = self.image.frame
    let originX = frm.origin.x
    let originY = frm.origin.y
    let originWidth = frm.size.width
    let originHeight = frm.size.height
    self.image.frame = frm

但是,如果我在这段代码中放入 originXoriginY

let path = UIBezierPath()
    path.moveToPoint(CGPoint(x: originX+50, y: originY))
    path.addCurveToPoint(CGPoint(x: originX-20, y: originY),
    controlPoint1: CGPoint(x: originX+20, y: originY),
    controlPoint2: CGPoint(x: originX-10, y: originY))

    let anim = CAKeyframeAnimation(keyPath: "position")

    // set the animations path to our bezier curve
    anim.path = path.CGPath

    anim.repeatCount = Float.infinity
    anim.duration = 5.0

    image.layer.addAnimation(anim, forKey: "animate position along path")

图像只停留在屏幕的左上角(而不是靠近中心)。为什么?顺便说一句,除了使用UIPathBezier之外,还有其他方法可以使图像流通吗?

嗯,首先你需要覆盖 UIBezierPath() 构造函数,这样你就可以给参数 originXoriginY,你也必须使用 var 而不是let因为var是一个简单的变量而let是一个constant并且在Swift中不能修改。

我没有 Xcode 但代码应该是这样的:

override func UIBezierPath(#x:CGFloat, andOriginX:CGFloat) {
}

let path = UIBezierPath(x, y)
    path.moveToPoint(CGPoint(x: x+50, y: y)) // and so one...
    path.addCurveToPoint(CGPoint(x: originX-20, y: originY),
    controlPoint1: CGPoint(x: originX+20, y: originY),
    controlPoint2: CGPoint(x: originX-10, y: originY))

    let anim = CAKeyframeAnimation(keyPath: "position")

    // set the animations path to our bezier curve
    anim.path = path.CGPath

    anim.repeatCount = Float.infinity
    anim.duration = 5.0
    image.layer.addAnimation(anim, forKey: "animate position along path")
)