2011-08-07 48 views
2

我有兩個觀點,一個是aView,另一個是bView,對aView一個按鈕,我按一下按鈕,我會跳轉到bView,請查看代碼CATransition是如何工作的?

-(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"]; 
} 

它不能很好地工作,它可以跳,所以我可以做什麼?感謝

回答

6

你忘了實際上是在結尾處添加視圖...

-(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]; 
} 

有,爲什麼你不使用的UIView動畫塊原因是什麼?這是因爲對於iOS的4動畫使用的UIView的transitionFromView的推薦方法:toView:持續時間:選擇:完成:應該爲你工作...

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

我用UIViewAnimationOptionTransitionCurlUp作爲過渡選項的例子,但你可以選擇任何UIViewAnimationOption。

+0

感謝您的回覆,但它仍然無法正常工作。兩種方式中的任何一種。仍然只顯示aView – matt

+0

視圖是否已正確初始化?如果您僅使用addSubview添加它,bView是否會添加到視圖中? – mjisrawi

+0

我已經完成了,非常感謝 – matt