I use 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")
However, two questions remain:
- I would like to move the image only partially along the path (according to the current time and sunrise / sunset) - how can this be done?
- The image will move to its original position after the animation stops - how can I avoid this?
Greetings
source share