Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting second tab stops animation in first tab and does not restart

I create a new tabbed application project in Xcode 4.2 using Storyboards. In viewDidLoad, I add the following code:

[UIView animateWithDuration:30
                      delay:0
                    options:UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear
                 animations:^(void) {
                     CGRect imageViewFrame = self.scrollingImageView.frame;
                     imageViewFrame.origin.x = CGRectGetWidth(imageViewFrame) / -2;
                     self.scrollingImageView.frame = imageViewFrame;
                 } completion:nil];

Then I create the associated UIImageView property and wire it up in IB.

@property (nonatomic, strong) IBOutlet UIImageView *scrollingImageView;

I put an image into the imageview, run it in the simulator or my iPod Touch, and I see scrolling.

However, when I select the second tab and then go back to the first tab, the animation completes what appears to be immediately and I cannot get it to restart, even if I put the above into viewWillAppear. I have searched through lots of answers and cannot solve it.

Any help would be appreciated.

like image 524
David Braun Avatar asked Jan 31 '12 03:01

David Braun


1 Answers

It appears that the issue you are dealing with is specific to tabbed bar controllers, not storyboards or the specific animation. The issue is that when you switch to a different tab, the animation stops, and when you return to the first tab, it doesn't resume.

Ordering the animation again in viewWillAppear does not work (as you mentioned), but if you first remove the animation in viewWillDisappear, then ordering it again in viewWillAppear should work. For example

- (void)viewWillDisappear:(BOOL)animated
{
    [self.view.layer removeAllAnimations]; // or whatever you need to use to remove your specific animation
}

NB: The animation removal code should be in viewWillDisappear rather than in viewWillUnload, since viewWillUnload does not get called when you switch tabs.

like image 193
auspicious99 Avatar answered Sep 20 '22 14:09

auspicious99