0
votes

I have code that runs fine for my animation. The problem is i want the whole animation to move to the left of the screen then stop and start again. Basically its a bear animating in place but i need the entire animation to move to the left across the screen. I dont want it to animate differently I just need it's x value to change but I'm stuck. Can someone please help. Here's what I have so far.

  • (void)viewDidLoad

{

[super viewDidLoad];

NSArray *imageNames = @[@"bear1.gif", @"bear2.gif", @"bear3.gif", @"bear4.gif",
                        @"bear5.gif", @"bear6.gif"];

NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) 
{

    [images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}

// Normal Animation

UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 50, 50)]; animationImageView.animationImages = images; animationImageView.animationDuration = 0.5;

[self.view addSubview:animationImageView];
[animationImageView startAnimating];
// Do any additional setup after loading the view, typically from a nib.

}

1

1 Answers

2
votes

Add this below the code that you have posted:

[UIView animateWithDuration:5 delay:1 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
    [UIView setAnimationRepeatCount:4];
    animationImageView.frame = CGRectMake(160, 95, 50, 50);
} completion:^(BOOL finished) {
    animationImageView.frame = CGRectMake(60, 95, 50, 50);
}];

This will move the bear to the right, along the x axis, taking 5 seconds to complete each animation cycle, and repeating the animation 4 times. It will also auto-reverse the animation to its original position after finishing each cycle.

If you don't want it to reverse smoothly and you rather have it snap back to original position after finishing each cycle, just remove the UIViewAnimationOptionAutoreverse flag.

Play around with the options shown to achieve the desired result.