Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - animating UIImageView image

just a quick issue I'm having regarding animating a moving background image for my current application setup. Essentially I want to have my currently still background image scroll from left to right endlessly across my view; but I'm struggling piecing it all together. From a variety of various other sources I've comprised this function that isn't currently working, and i really can't 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/assistance is immensely appreciated!

like image 302
Jordy Avatar asked Jul 03 '26 10:07

Jordy


1 Answers

You don't need to use CAffineTranform inside animateWithDuration, you can simply define the 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.

like image 191
Shubham Naik Avatar answered Jul 04 '26 23:07

Shubham Naik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!