我正在使用此代码沿曲线移动UIImage:
// paint curve for sun
let path = UIBezierPath()
let imageSunName = "small_sun.png"
let imageSun = UIImage(named: imageSunName)
let imageView = UIImageView(image: imageSun!)
imageView.frame = CGRect(x: xStart, y: yStart, width: 24, height: 24)
self.view.addSubview(imageView)
path.moveToPoint(CGPoint(x: xStart,y: yStart))
path.addQuadCurveToPoint(CGPoint(x: xEnd, y: yEnd), controlPoint: CGPoint(x: xMiddleTop, y: yMiddleTop))
let animation = CAKeyframeAnimation(keyPath: "position")
animation.path = path.CGPath
animation.repeatCount = 0
animation.duration = 5.0
imageView.layer.addAnimation(animation, forKey: "animate position along path")
尽管如此,还有两个问题:
干杯
答案 0 :(得分:6)
为避免跳到原来的位置,请添加:
animation.fillMode = kCAFillModeForwards
animation.removedOnCompletion = false
答案 1 :(得分:0)
为避免跳过动画结束,在开始动画之前,将图像层移动到结束位置:
imageView.layer.position = CGPoint(x: xEnd, y: yEnd)
部分移动图像我担心没有简单的方法可以做到这一点。另一种方法是通过计算endPosition和基于日落/日出的路径,这样你就不必把动画时间弄得一团糟