TheCesco88
TheCesco88

Reputation: 141

Swift move image from right to left than from left to right

I want to make an animation that move a background image from right to left than left to right. I use this code but not works

        UIView.animate(withDuration: 5, delay: 0, options: .repeat ,animations: { () -> Void in
                imageView.transform = CGAffineTransform(translationX: -imageView.frame.width + self.view.bounds.width, y: 0)
                UIView.animate(withDuration: 5, delay: 1, options: .repeat ,animations: { () -> Void in
                    imageView.transform = CGAffineTransform(translationX: +imageView.frame.width - self.view.bounds.width, y: 0)

                })

            })

Upvotes: 1

Views: 3583

Answers (2)

TheCesco88
TheCesco88

Reputation: 141

I solved with this

UIView.animate(withDuration: 30.0, delay: 0, options: [.repeat, .autoreverse], animations: {

                imageView.transform = CGAffineTransform(translationX: -imageView.frame.width + self.view.bounds.width, y: 0)

            }, completion: nil)

Upvotes: 3

charlyatwork
charlyatwork

Reputation: 1197

Use the following code if you want to use the legacy animate(withDuration:animations:) API. However, you need to wait for the first animation to be finished - this means you have to use the completion block. However, your value for translationX does not makes sense as well.

UIView.animate(withDuration: 1, animations: {
            self.imageView.transform = CGAffineTransform(translationX: -self.imageView.frame.width, y: 0)
        }) { _ in
            UIView.animate(withDuration: 1) {
                self.imageView.transform = CGAffineTransform.identity
            }
        }

I would strongly recommend having a look at the "new" way of doing Animations: UIViewPropertyAnimator(https://developer.apple.com/documentation/uikit/uiviewpropertyanimator)

Upvotes: 0

Related Questions