Search code examples
swiftxcodeanimationuiviewanimationcgaffinetransform

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)

                })

            })

Solution

  • 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)