How to use block for UIView animation?

Discussion in 'iOS Programming' started by mikezang, Nov 20, 2012.

  1. mikezang, Nov 20, 2012
    Last edited: Nov 20, 2012

    macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    I have code as below to do UIView animation, but I found that doesn't work in iOS6, I tried to use block but unsuccessful, does anyone help me?
    Code:
    - (void)animationDidStop:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context {
        UIView* backView = (UIView*)context;
        [backView removeFromSuperview];
        [backView release];
    }
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
        UIImage* backImage = [UIImage imageNamed:@"Default.png"];
        UIView* backView = [[UIImageView alloc] initWithImage:backImage];
        backView.frame = window.bounds;
        [window addSubview:backView];
        [UIView beginAnimations:@"CWFadeIn" context:(void*)backView];
        [UIView setAnimationDuration:1];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector: @selector(animationDidStop:finished:context:)];
        backView.alpha = 0;
        [UIView commitAnimations];
    
        [window makeKeyAndVisible];
        return YES;
    }
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Doesn't work how? You need to be more specific.

    It might help if you showed us the code of trying to use blocks.

    P.S. I'm not sure putting animation code in your didFinishLaunchingWithOptions: is the best place for it. Probably should go in the viewDidAppear: of your first viewController.
     
  3. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #3
    I use this code to make a fade out effect so that should be on startup, in iOS5, you can see that animation, but there is no such effect in iOS6, so I think something is changed in iOS6, you can copy it to your any app for testing both iOS5.1 and iOS6 simulator, you will find the difference.
     
  4. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    You are using iOS 3 Animations, have you tried using UIView animatin block instead?
    [UIView animateWithDuration:blah blah]?
     
  5. dejo, Nov 20, 2012
    Last edited: Nov 20, 2012

    Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Look like he already did:

    I tried that code in both iOS 5.1 and 6.0 Simulator and in both cases I saw no fade-out; there was no difference.

    EDIT:
    Oops, forgot to include the animationDidStop: method. With that added, I get a compile-time error ("Cast of C pointer type 'void *' to Objective-C pointer type 'UIView *' requires a bridged cast") on this line:
    Code:
    UIView* backView = (UIView*)context;
     
  6. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #6
    This app was created before ARC, so you need to use no objc compile flag..
     
  7. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #7
    I changed it to block as below, but I found this effect is only happened in iOS5 not iOS6, do you know why?
    Code:
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
        UIImage* backImage = [UIImage imageNamed:@"Default.png"];
        UIView* backView = [[UIImageView alloc] initWithImage:backImage];
        backView.frame = window.bounds;
        [window addSubview:backView];
        
        [UIView animateWithDuration:1 animations:^{
            backView.alpha = 0;
        } completion:^(BOOL finished){
            [backView removeFromSuperview];
        }];
    	
        [backView release];
    }
    
     
  8. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #8
    Hi, I found the reason base on Times of calling application:didFinishLaunchingWithOptions: differs between iOS 5.x and iOS 6, because sequence is different in iOS5 and iOS6.
    In iOS 5.x I have the next consequence of methods calling:
    Code:
    -[UIViewController initWithCoder:]
    -[UIApplicationDelegate didFinishLaunchingWithOptions:]
    -[UIViewController viewDidLoad]
    -[UIViewController viewWillAppear:]
    -[UIViewController viewDidAppear:]
    
    But in iOS 6.x it's the next:
    Code:
    -[UIViewController initWithCoder:]
    -[UIViewController viewDidLoad]
    -[UIViewController viewWillAppear:]
    -[UIApplicationDelegate didFinishLaunchingWithOptions:]
    -[UIViewController viewDidAppear:]
    
    so I move my code from didFinishLaunchingWithOptions to viewDidLoad as below, now both fade out in iOS5 and iOS6:
    Code:
    - (void)viewDidLoad {
        [super viewDidLoad];
    	
        UIImage* backImage = [UIImage imageNamed:@"Default.png"];
        UIImageView* backView = [[UIImageView alloc] initWithImage:backImage];
        backView.frame = self.view.bounds;
        [self.view addSubview:backView];
        
        [UIView animateWithDuration:1.0
             animations:^{backView.alpha = 0.0;}
             completion:^(BOOL finished) {[backView removeFromSuperview];}
         ];
    	
        [backView release];
    }
    
     
  9. Reason077, Nov 21, 2012
    Last edited by a moderator: Nov 21, 2012

    macrumors 68000

    Reason077

    Joined:
    Aug 14, 2007
    #9
    As dejo suggests, you should really put this sort of thing in viewDidAppear. viewDidLoad is not guaranteed to only run when the application starts - views can be loaded and unloaded multiple times depending on memory availability, etc.

    You can use a boolean flag to make sure it only runs once at startup, ie:

    Code:
    if (!animationShown)
      {
         animationShown = YES;
         // ... do animation ...
      }
     
  10. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #10
    My bad Dejo, I read it, but I thought he misunderstood what Block meant.
    That block looks fine for me, should work to be honest, I use animation blocks in all my apps, with support back to 4.3, so why it's not working in 6 is a mystery for me, sorry.
    I think it has to do with the applicationDidFinishLaunching, that you should wait with doing that, add a VC in between which does the animation, and not the appdelegate.
     
  11. macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #11

    Are you using Storyboards in iOS 5 and XIBs in iOS 6 or something? Or is your initialization code different on the different OS versions? It seems odd that the order of the setup calls would be different like that, and seems like it would break a lot of programs in the field.
     
  12. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #12
    Well, my app used nib file, it seems like storyboard in iOS 6.
     
  13. macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #13
    What do you mean "...it seems like storyboard in iOS 6." If you did not add a storyboard file to your project, and write specific storyboard code in your app, you're not using Storyboards. There's no ambiguity about it. You either are or you aren't.
     
  14. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #14
    Sorry, it is my misunderstanding, like you said, my app is not use storyboard.
     

Share This Page