问题使用CATransitions切换视图

时间:2010-12-22 07:10:10

标签: iphone catransition

所以我试图使用此代码在视图之间切换:

CATransition *applicationLoadViewIn = [CATransition animation];

    [applicationLoadViewIn setDuration:1];

    [applicationLoadViewIn setType:kCATransitionReveal];

    [applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];

    MyViewControllerClass *theControllerIAmSwitchingTo = [[MyViewControllerClass alloc] init];

    [[theControllerIAmSwitchingTo.view layer] addAnimation:applicationLoadViewIn forKey:kCATransitionReveal];

但是,代码不起作用。没有错误,代码执行正常,但它没有做任何事情。我有什么不对吗?

2 个答案:

答案 0 :(得分:1)

您需要将动画添加到父视图层,然后将视图添加为父视图的子视图。

所以而不是

[[theControllerIAmSwitchingTo.view layer] addAnimation:applicationLoadViewIn forKey:kCATransitionReveal];

您需要使用

[parentView.layer addAnimation:animation forKey:kCATransitionReveal];
[parentView addSubview:theControllerIAmSwitchingTo.view];

答案 1 :(得分:1)

就像这样:

 CATransition *applicationLoadViewIn = [CATransition animation];

    [applicationLoadViewIn setDuration:1];

    [applicationLoadViewIn setType:kCATransitionReveal];

    [applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];

    MyViewControllerClass *theControllerIAmSwitchingTo = [[MyViewControllerClass alloc] init];

    [[theControllerIAmSwitchingTo.view layer] addAnimation:applicationLoadViewIn forKey:kCATransitionReveal];

    [self.navigationController pushViewController:theControllerIAmSwitchingTo animated:NO];

我注意到问题是你没有在导航堆栈中添加你的视图,这就是为什么你的视图没有被加载而你看不到动画。