I am using this code to move a UIImage along a curve:
// 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")
Nevertheless, two issues remain:
- I would like to move the image only partly along the path (according to the current time vs sunrise/sunset) - how can this be done?
- The image jumps to its origin position after the animation stopped - how can I avoid this?
Cheers