Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 20, 2012, 06:51 AM   #1
mikezang
macrumors 6502a
 
Join Date: May 2010
Location: Tokyo, Japan
How to use block for UIView animation?

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;
}
__________________
iPad Mini 32GB, iPhone 4S 16GB, iPad1 32GB, Mac mini 2.53GHz 4GB 320GB, ThinkPad T60
MyApp: Market Event v1.9.2

Last edited by mikezang; Nov 20, 2012 at 08:20 AM.
mikezang is offline   0 Reply With Quote
Old Nov 20, 2012, 08:14 AM   #2
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by mikezang View Post
I have code as below to do UIView animation, but I found that doesn't work in iOS6...
Doesn't work how? You need to be more specific.

Quote:
...I tried to use block but unsuccessful, does anyone help me?
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.
__________________
dejo is offline   0 Reply With Quote
Old Nov 20, 2012, 08:19 AM   #3
mikezang
Thread Starter
macrumors 6502a
 
Join Date: May 2010
Location: Tokyo, Japan
Quote:
Originally Posted by dejo View Post
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.
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.
__________________
iPad Mini 32GB, iPhone 4S 16GB, iPad1 32GB, Mac mini 2.53GHz 4GB 320GB, ThinkPad T60
MyApp: Market Event v1.9.2
mikezang is offline   0 Reply With Quote
Old Nov 20, 2012, 03:13 PM   #4
jnoxx
macrumors 65816
 
jnoxx's Avatar
 
Join Date: Dec 2010
Location: Aartselaar // Antwerp // Belgium
You are using iOS 3 Animations, have you tried using UIView animatin block instead?
[UIView animateWithDuration:blah blah]?
__________________

iPad Mini, iPad 4, iPad 2, iPhone 3G,4,5, iMac 24", Mac Mini Last gen, Macbook Pro Retina with Dell U2711
jnoxx is offline   0 Reply With Quote
Old Nov 20, 2012, 06:36 PM   #5
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by jnoxx View Post
...have you tried using UIView animatin block instead?
Look like he already did:

Quote:
Originally Posted by mikezang View Post
...I tried to use block but unsuccessful...
Quote:
Originally Posted by mikezang View Post
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.
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;
__________________

Last edited by dejo; Nov 20, 2012 at 06:56 PM. Reason: Consecutive posts, added Edit.
dejo is offline   0 Reply With Quote
Old Nov 20, 2012, 11:18 PM   #6
mikezang
Thread Starter
macrumors 6502a
 
Join Date: May 2010
Location: Tokyo, Japan
This app was created before ARC, so you need to use no objc compile flag..
mikezang is offline   0 Reply With Quote
Old Nov 21, 2012, 05:13 AM   #7
mikezang
Thread Starter
macrumors 6502a
 
Join Date: May 2010
Location: Tokyo, Japan
Quote:
Originally Posted by jnoxx View Post
You are using iOS 3 Animations, have you tried using UIView animatin block instead?
[UIView animateWithDuration:blah blah]?
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];
}
__________________
iPad Mini 32GB, iPhone 4S 16GB, iPad1 32GB, Mac mini 2.53GHz 4GB 320GB, ThinkPad T60
MyApp: Market Event v1.9.2
mikezang is offline   0 Reply With Quote
Old Nov 21, 2012, 05:38 AM   #8
mikezang
Thread Starter
macrumors 6502a
 
Join Date: May 2010
Location: Tokyo, Japan
Quote:
Originally Posted by dejo View Post
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;
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];
}
__________________
iPad Mini 32GB, iPhone 4S 16GB, iPad1 32GB, Mac mini 2.53GHz 4GB 320GB, ThinkPad T60
MyApp: Market Event v1.9.2
mikezang is offline   0 Reply With Quote
Old Nov 21, 2012, 06:14 AM   #9
Reason077
macrumors 65816
 
Join Date: Aug 2007
Quote:
Originally Posted by mikezang View Post
so I move my code from didFinishLaunchingWithOptions to viewDidLoad as below, now both fade out in iOS5 and iOS6:
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 ...
  }

Last edited by dejo; Nov 21, 2012 at 09:09 AM. Reason: Please use [code] tags.
Reason077 is online now   0 Reply With Quote
Old Nov 21, 2012, 08:02 AM   #10
jnoxx
macrumors 65816
 
jnoxx's Avatar
 
Join Date: Dec 2010
Location: Aartselaar // Antwerp // Belgium
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.
__________________

iPad Mini, iPad 4, iPad 2, iPhone 3G,4,5, iMac 24", Mac Mini Last gen, Macbook Pro Retina with Dell U2711
jnoxx is offline   0 Reply With Quote
Old Nov 21, 2012, 10:10 PM   #11
Duncan C
macrumors 6502a
 
Duncan C's Avatar
 
Join Date: Jan 2008
Location: Northern Virginia
 
Quote:
Originally Posted by mikezang View Post
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];
}

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.
__________________
Regards,
Duncan Champney, WareTo.
Check out our latest iOS app, Face Dancer, available for free on the App Store.
Duncan C is offline   0 Reply With Quote
Old Nov 22, 2012, 01:20 AM   #12
mikezang
Thread Starter
macrumors 6502a
 
Join Date: May 2010
Location: Tokyo, Japan
Quote:
Originally Posted by Duncan C View Post
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.
Well, my app used nib file, it seems like storyboard in iOS 6.
__________________
iPad Mini 32GB, iPhone 4S 16GB, iPad1 32GB, Mac mini 2.53GHz 4GB 320GB, ThinkPad T60
MyApp: Market Event v1.9.2
mikezang is offline   0 Reply With Quote
Old Nov 24, 2012, 07:57 PM   #13
Duncan C
macrumors 6502a
 
Duncan C's Avatar
 
Join Date: Jan 2008
Location: Northern Virginia
 
Quote:
Originally Posted by mikezang View Post
Well, my app used nib file, it seems like storyboard in iOS 6.
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.
__________________
Regards,
Duncan Champney, WareTo.
Check out our latest iOS app, Face Dancer, available for free on the App Store.
Duncan C is offline   0 Reply With Quote
Old Nov 24, 2012, 11:58 PM   #14
mikezang
Thread Starter
macrumors 6502a
 
Join Date: May 2010
Location: Tokyo, Japan
Quote:
Originally Posted by Duncan C View Post
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.
Sorry, it is my misunderstanding, like you said, my app is not use storyboard.
__________________
iPad Mini 32GB, iPhone 4S 16GB, iPad1 32GB, Mac mini 2.53GHz 4GB 320GB, ThinkPad T60
MyApp: Market Event v1.9.2
mikezang is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Will block app cellular usage also block wi-fi? Snesley Wipes iOS 7 4 Sep 19, 2013 09:03 AM
How to navigate from UIView to UIViewController fypj2013 iPhone/iPad Programming 6 Mar 25, 2013 04:37 PM
Trying to subclass UIView Hans Kamp iPhone/iPad Programming 3 Mar 25, 2013 12:15 AM
Archiving Custom UIView Ides iPhone/iPad Programming 14 Aug 8, 2012 05:44 PM
Simple UIView Question krayziekray iPhone/iPad Programming 5 Jun 19, 2012 05:17 AM

Forum Jump

All times are GMT -5. The time now is 07:00 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC