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

时间:2015-12-15 10:36:29

标签: ios swift animation path

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

让我们看看图片:

problem visual explanation

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?

1 个答案:

答案 0 :(得分:2)

使用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 question帮我形成了这个答案,如果一切都失败了,你总是可以参考docs