Swift - Animated UIImageView

just a quick question I have regarding animating a moving background image for my current app setup. Basically, I want my current background image to scroll from left to right infinitely in my view; but I'm struggling to put it all together. From a variety of other sources, I have included this feature, which is currently not working and I really cannot figure out how or why.

func animate(_ image: UIImageView) {
        UIView.animate(withDuration: 5, delay: 0, options: .curveLinear, animations: {
            image.transform = CGAffineTransform(translationX: -100, y: 0)
        }) { (success: Bool) in
            image.transform = CGAffineTransform.identity
            self.animate(image)
        }
    }

      

Any help / help is greatly appreciated!

+3


source to share


1 answer


You don't need to use CAffineTranform

internally animateWithDuration

, you can just define a new center like:

let oldCenter = image.center
let newCenter = CGPoint(x: oldCenter.x - 100, y: oldCenter.y)

UIView.animate(withDuration: 1, delay: 0, options: .curveLinear, animations: {
  image.center = newCenter
}) { (success: Bool) in
  print("Done moving image")
  }

      



Hope it helps.

+2


source







All Articles