1

Is there a way to animate an UIImageView from right to left without using CABasicAnimation? I'm having troubles with it and I would like to find another way to do it.

The "ground" image its 1200 (width) x 33 (height) and I would like to move it from right to left simulating an endless runner with infinite repetitions.

How can I achieve it without CALayer/CABasicAnimation?.

Thank you!

iDec
  • 707
  • 2
  • 8
  • 16

2 Answers2

1

There's a bunch of methods on UIView that allows any animations, particularly moving views.

You could move your imageView like this:

[UIView animateWithDuration:10
                 animations:^{

                     yourImageView.frame = frame_you_need;
                 }
                 completion:^(BOOL finished){

                    //start from the very beginning
                    //set initial frame to the image view and run the 
                    // animation again
                 }];

so you need two image views. set them start positions, run animations on both, in completion of animation repeat the animation again. that's it

UPDATE

I decided to implement this functionality myself. Here's how my animating function looks:

- (void)runView:(UIView *)aView withStartPosition:(CGPoint)startPosition
{
aView.center = startPosition;

[UIView animateWithDuration:3
                      delay:0
                    options:UIViewAnimationOptionCurveLinear
                 animations:^{

                     CGPoint newPosition = CGPointMake(aView.center.x + aView.frame.size.width, aView.center.y);
                     aView.center = newPosition;
                 }
                 completion:^(BOOL finished){

                     [self runView:aView withStartPosition:startPosition];

                 }];
}

The project demonstrating this functionality is here: Github

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
  • Awesome! The project on GitHub helped me a lot, I understand things better if I can see them :) Thanks! – iDec Apr 02 '16 at 09:52
0

Since UIView.animateWithDuration is sometimes awkward to implement, I created a way to perform animations, with sequences and groups easier. You can check it out here: GitHub - UIAnimation . It's the UIKit version of SKAction.

Using this UIAnimation a way to implement what you want would be:

//imgView is your UIImageView
let positioning = UIAnimation.moveTo(CGPointMake(view.frame.width+imgView.frame.width/2,view.frame.height/2), duration: 0) //put the UIView on the right side of the screen
let movement = UIAnimation.moveTo(CGPointMake(-imgView.frame.width/2),view.frame.height/2), duration: 5) //moves the UIView to the left side of the screen
let animation = UIAnimation.sequence([positioning,movement]) //do them sequentially
let foreverAnimation = UIAnimation.repeatAnimationForever(animation) //repeat the sequence forever
imgView.runAnimation(foreverAnimation) //start the animation

The interesting thing here is that you can store the 'foreverAnimation' in a variable and reuse it for all the imageViews you want to move. You just need to call runAnimation.

Hope it helps. If you have any problems or suggestions regarding the script, contact me please =)

Pietro Pepe
  • 389
  • 2
  • 8