沿路径放置对象然后移动它(包含IMG)

我有一个问题(;)),我需要你的帮助。

让我们看看图像:

问题视觉解释

1)我有一条路。 让我们说它是这样的:

let bezierPath = UIBezierPath() bezierPath.moveToPoint(CGPointMake(10.5, 47.5)) bezierPath.addCurveToPoint(CGPointMake(45.5, 23.5), controlPoint1: CGPointMake(10.5, 47.5), controlPoint2: CGPointMake(32.5, 23.5)) bezierPath.addCurveToPoint(CGPointMake(84.5, 47.5), controlPoint1: CGPointMake(58.5, 23.5), controlPoint2: CGPointMake(84.5, 47.5)) bezierPath.addLineToPoint(CGPointMake(10.5, 47.5)) bezierPath.closePath() UIColor.redColor().setStroke() bezierPath.lineWidth = 1 bezierPath.stroke() 

2)我有一个UIImageView。 第一个问题是:如何将它放在路径的指定部分(A点)的顶部?

3)第二个问题:如何从A点到B点进行动画处理?

通过使用CAKeyframeAnimation ,您可以使用您创建的路径创建动画

 let animation = CAKeyframeAnimation() // Could also be position.x or position.y if you want to animate a separate axis. animation.keyPath = "position" animation.repeatCount = 0 // How many times to repeat the animation animation.duration = 5.0 // Duration of a single repetition animation.path = bezierPath.CGPath 

然后将其附加到图像的图层

 imageView.layer.addAnimation(animation, forKey: "move image along bezier path") 

这个其他stackoverflow问题帮助我形成了这个答案,如果所有其他方法都失败了,你总是可以参考文档 。