Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Animation one after another...

tooomtooom Posts: 58Registered Users
edited November 2010 in iOS SDK Development
I have three CALayers and I would like to do something like this

layer1.hidden = NO;
layer1.opacity = 0.0f; //fade out

layer2.hidden = NO;
layer2.opacity = 0.0f;

layer3.hidden = NO;
layer3.opacity = 0.0f;

Now that actually works fine and in runs fine. The problem with that type of code would be that the three layers would fade out simultaneously. But I want them to fade out after the previous layer has faded out, ie. layer2 should appear and fade out after layer1 has faded out and not simultaneously.

Can anybody give me a hint how I can do such "dependent" animations?
Post edited by tooom on

Replies

  • akacakac Posts: 31Registered Users
    edited November 2010
    Using 4.0 animation completion blocks you nest them. Makes it nice and simple. Using 2/3.x delegate calls, you set a completion selector with a delegate and have the animations nested by that.
  • tooomtooom Posts: 58Registered Users
    edited November 2010
    Thanks for the answer. I tried this here:
    -(void)animate {
    	
    	[UIView animateWithDuration:1.0f
    			animations:^{ layer1.opacity = 0.0; NSLog(@"1"); }
    			completion:^(BOOL finished){ 
    					[UIView animateWithDuration:1.0f
    								 animations:^{ layer1.opacity = 1.0; NSLog(@"2"); }];
    			}
    	];
    	
    }
    
    But still the animations occur simultaneously. Then I modified the program like this
    -(void)animate {
    	
    	[UIView animateWithDuration:1.0f
    			animations:^{ layer1.opacity = 0.0; NSLog(@"1"); }
    			completion:^(BOOL finished){ 
    				@selector(animate2);
    					/*[UIView animateWithDuration:1.0f
    								 animations:^{ layer1.opacity = 1.0; NSLog(@"2"); }];*/
    			}
    	];
    	
    }
    
    -(void)animate2 {
    	[UIView animateWithDuration:1.0f
    					 animations:^{ layer1.opacity = 1.0; NSLog(@"2"); }
    					 
    	 ];
    }
    

    But it does not work either. Could you provide same simple sample code of how it has to be done? Thanks in advance.
  • tooomtooom Posts: 58Registered Users
    edited November 2010
    Okay I fixed it. The error I made was that I did not animate a UIView but a CALayer. That's obviously not possible. The code looks like this now:
    @interface SequentialAnimationViewController : UIViewController {
    
    	...
    	UIView* layer1View;
    	CALayer* layer1;
    	...
    }
    

    And in the implementation file
    - (void)viewDidLoad {
    	layer1 = [[CALayer alloc] init];
    	...
    	layer1View = [[UIView alloc] init];
    	[self.view addSubview:layer1View];
    	[layer1View.layer insertSublayer:layer1 above:layer1View.layer];
    	...
    }
    
    ...
    -(void)animate {
    	
    	
    	[UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionCurveLinear
    			animations:^{ layer1View.alpha = 0.0f; NSLog(@"1"); }
    			completion:^(BOOL finished){ 
    					[UIView animateWithDuration:1.0f delay:1.0f options:UIViewAnimationOptionCurveLinear 
    							animations:^{
    								layer1View.alpha = 1.0f;
    								NSLog(@"2");
    							} 
    							completion:nil
    					];
    			}
    	];
    	
    }
    

    Note that in the previous version I didn't animate a UIView but only a CALayer. Now I've embedded the CALayer into a UIView (layer1View) and I animated this UIView. Now it works fine.
Sign In or Register to comment.