Search code examples
swiftanimationuiimageview

How i move an image from left to right in swift?


I did that...

Screen Shot

ivImageLoadingInside.center = CGPoint(x: 25, y: 0)
   UIView.animate(withDuration: 2, delay: 0, options: .repeat, animations: {
   self.ivImageLoadingInside.center = CGPoint(x: 80, y: 0)
}, completion: nil)

But always stops in the middle.

Here is the image with the animation

Screen Shot


Solution

  • I got it! You have to put the constraints and do this...

    if animate { ivImageLoadingInside.frame = CGRect(x: 0 - 160, y: 123, width: 160, height: 12)

            UIView.animate(withDuration: 1.2, delay: 0, options: .repeat, animations: {
                self.ivImageLoadingInside.frame = CGRect(x: ScreenSize.SCREEN_WIDTH, y: 123, width: 160, height: 12)
    
            }, completion: nil)
        } else {
            ivImageLoadingInside.layer.removeAllAnimations()
        }*
    

    Look at this link http://mathewsanders.com/prototyping-iOS-iPhone-iPad-animations-in-swift/