how does the CATransition work?

Bin picture Bin · Aug 7, 2011 · Viewed 10.7k times · Source

i have two views,one is aView,another is bView, there is a button on aView,i click the button ,i will jump to bView,please see the code

-(IBAction)jump2b:(id)sender{
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 0.25f;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
animation.type = kCATransitionPush;
animation.subtype = kCATransitionFromTop;
   [bView.view.layer addAnimation:animation forKey:@"animation"];
 }

it can not work well,it can jump,so what can i do?thanks

Answer

mjisrawi picture mjisrawi · Aug 7, 2011

You forgot to actually add the view at the end...

-(IBAction)jump2b:(id)sender{
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = 0.25f;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromTop;
    [bView.view.layer addAnimation:animation forKey:@"animation"];

    //add bView to current view
    [self.view addSubview:bView];
}

Is there reason why you're not using UIView animation blocks? It's the recommended method for animations since iOS 4. Using UIView's transitionFromView:toView:duration:options:completion: should work for you...

[UIView transitionFromView:aView 
                    toView:bView 
                  duration:0.25 
                   options: (UIViewAnimationCurveEaseInOut | UIViewAnimationOptionTransitionCurlUp)
                completion:nil];

I used UIViewAnimationOptionTransitionCurlUp as an example for the transition option, but you can choose any UIViewAnimationOption.