Swift: moving a UIImage partially along a path

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

+5
source share
2 answers

To avoid a reset, add:

 animation.fillMode = kCAFillModeForwards animation.removedOnCompletion = false 
+5
source

To avoid moving to the end of the animation, before starting the animation, move the image layer to the end position:

 imageView.layer.position = CGPoint(x: xEnd, y: yEnd) 

To partially move the image, I got scared that there is no easy way to do this. An alternative way would be to calculate the endPosition and the sunset / sunrise based path so you don't have to ruin the animation at all

0
source

All Articles