xcode button rotation

Discussion in 'iOS Programming' started by DavidG98, Mar 9, 2013.

  1. DavidG98, Mar 9, 2013
    Last edited by a moderator: Mar 9, 2013

    DavidG98 macrumors newbie

    Joined:
    Mar 9, 2013
    #1
    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!
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    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.
     
  3. DavidG98 thread starter macrumors newbie

    Joined:
    Mar 9, 2013
    #3
    I've tried putting the code in the viewDidLoad, calling it in viewDidAppear but nothing seems to work
     
  4. TouchMint.com macrumors 68000

    TouchMint.com

    Joined:
    May 25, 2012
    Location:
    Phoenix
    #4
    toss and nslog in there to see if its happening or toss that code into an action you know works.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    You might need to trigger the method from your application delegate's applicationWillEnterForeground: or applicationDidBecomeActive:.
     
  6. DavidG98 thread starter macrumors newbie

    Joined:
    Mar 9, 2013
    #6
    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?
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    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.
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    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.
     
  9. DavidG98 thread starter macrumors newbie

    Joined:
    Mar 9, 2013
    #9
    How would I go about implementing the UIApplicationWillEnterForegroundNotification thing? I've gone online but can't find the answer anywhere.
     
  10. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #10
    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];
    }
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    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
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    If you're gonna remove the observer in the dealloc, I'd suggest adding it in init.
     
  13. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #13
    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.
     
  14. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #14
    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.
     

Share This Page