Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DavidG98

macrumors newbie
Original poster
Mar 9, 2013
4
0
Hi there,

I'm having some difficulties with the animation. I want it to rotate when i launch the app (which it does). I also want it to work when I exit and reenter the app but it just stays there when I re enter the app. Here is my code:
Code:
- (void)viewDidAppear:(BOOL)animated{
    [self spin];
}
-(void)spin{
    
    
      [UIView commitAnimations];
   [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationRepeatCount:-1];
    [UIView setAnimationDuration:5.0];
    
         spin.transform = CGAffineTransformMakeRotation((M_PI/180)*180);
}

Any help would be greatly appreciated! Thanks!
 
Last edited by a moderator:
I don't believe viewDidAppear: is called if you just exit and reenter the app.

P.S. You should also be calling your super's viewDidAppear:, just in case.
 
I've tried putting the code in the viewDidLoad, calling it in viewDidAppear but nothing seems to work
 
You might need to trigger the method from your application delegate's applicationWillEnterForeground: or applicationDidBecomeActive:.
 
Ive tried doing it in the app delegate, this is my code:
Code:
  ViewController * vc = [[ViewController alloc]init];
    [vc spin];
It doesn't call the method spin. What shall I do?
 
It doesn't call the method spin. What shall I do?

I'm pretty sure it will call the spin method (try putting in a breakpoint to confirm). The problem is: because you just instantiated a brand new viewController, it hasn't even had a chance to load its views, so any kind of animation gets lost. You'll want to find a way to trigger this method on a pre-existing viewController, either by saving a reference to it, or using an NSNotification, or some other means.
 
Your view controller needs to register for one of the notifications listed at the bottom of UIApplication.h. UIApplicationWillEnterForegroundNotification or UIApplicationDidBecomeActiveNotification most likely. Then call your spin method from there.
 
How would I go about implementing the UIApplicationWillEnterForegroundNotification thing? I've gone online but can't find the answer anywhere.
 
In your viewDidLoad you place something like the following into it. You typically only want that line execute once.
Code:
[[NSNotificationCenter defaultCenter] addObserver: self
            selector: @selector(applicationDidBecomeActive:)
            name: UIApplicationDidBecomeActiveNotification object:nil];

You also need to place a new instance method in the class that looks like the following.

Code:
- (void) applicationDidBecomeActive: (UIApplication *)application
{
  // your code here.
}

Finally, you want to remove the object as an observer when it gets deallocated, so in you need the following at minimum.

Code:
- (void) dealloc
{
	[[NSNotificationCenter defaultCenter] removeObserver:self];
}
 
Open the Xcode Organizer window and the documentation pane. Enter NSNotification in the search box. Read "Registering for a Notification." Also, look at any of the sample code.

@xStep, should be

Code:
-(void)applicationDidBecomeActive:(NSNotification*)notification
 
Good point. And if one places the addObserver in the viewDidLoad method, than balancing that out would mean to place the removal into the viewDidUnload method.

Except that Apple made that impossible. The viewDidUnload method is deprecated in iOS 6, and no longer called. This means that you can no longer put balancing setup/tear down code in viewDidLoad and viewDidUnload.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.