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

Upvotes: 1

Views: 10661

Answers (3)

Giang
Giang

Reputation: 3655

private func animation(viewAnimation: UIView) {
    UIView.animate(withDuration: 2, animations: {
        viewAnimation.frame.origin.x = +viewAnimation.frame.width
    }) { (_) in
        UIView.animate(withDuration: 2, delay: 1, options: [.curveEaseIn], animations: {
            viewAnimation.frame.origin.x -= viewAnimation.frame.width
        })

    }
}

Upvotes: 9

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/

Upvotes: -1

Yakiv Kovalskyi
Yakiv Kovalskyi

Reputation: 1757

self.ivImageLoadingInside.center = CGPoint(x: 80 - self.ivImageLoadingInside.bounds.width / 2, y: 0)

Upvotes: 0

Related Questions