iPhone - UINavigationController - 使用CATransaction的自定义动画

时间:2010-10-28 15:06:06

标签: animation uinavigationcontroller catransition

我正在尝试为UINavigationController创建一个类别,因此我可以组合一些自定义动画。我遇到了this question,它让我开始了。我推出了以下推送功能:

- (void)pushViewControllerMoveInFromBottom:(UIViewController *)viewController {
    [CATransaction begin];
    CATransition *transition;
    transition = [CATransition animation];
    transition.type = kCATransitionMoveIn;
    transition.subtype = kCATransitionFromBottom;
    transition.duration = 0.7;

    [CATransaction setValue:(id)kCFBooleanTrue
                     forKey:kCATransactionDisableActions];

    [[[[self.view subviews] objectAtIndex:0] layer] addAnimation:transition forKey:nil];
    [self  pushViewController:viewController animated:YES];
    [CATransaction commit];
}

我不明白通过子视图调用访问了哪个视图/层。所以,如果有人能帮助我理解这一点,那将会有所帮助。它似乎正在影响即将进入屏幕的任何视图。

但是我的主要问题是我正在尝试制作一个能够“撤消”上述功能的流行音乐。为了清楚起见,我可以说我有两个视图,A和B. A是屏幕上显示的第一个视图。上面的函数将在B中移动而不会将A推出。就像模态视图一样。但是下面的函数将A滑回到B上,而不是将B向上滑动并将A留在原位。

- (void)popViewControllerMoveInFromTop {
    [CATransaction begin];
    CATransition *transition;
    transition = [CATransition animation];
    transition.type = kCATransitionMoveIn;
    transition.subtype = kCATransitionFromTop;
    transition.duration = 0.7;

    [CATransaction setValue:(id)kCFBooleanTrue
                     forKey:kCATransactionDisableActions];

    [[[[self.view subviews] objectAtIndex:0] layer] addAnimation:transition forKey:nil];
    [self  popViewControllerAnimated:YES];    
    [CATransaction commit];
}

我不想使用模态视图,我宁愿不使用自定义动画块来更改视图的框架。

1 个答案:

答案 0 :(得分:3)

我已经完成了类似的事情,虽然我没有费心将它转移到它自己的功能中:

CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromBottom;
[self.navigationController.view.layer addAnimation:transition forKey:nil];

[self.navigationController popViewControllerAnimated:NO]

你可以抽象出来。使用您的代码,它看起来像:

- (void)popViewControllerMoveInFromTop {
[CATransaction begin];
CATransition *transition;
transition = [CATransition animation];
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromTop;
transition.duration = 0.7;

[CATransaction setValue:(id)kCFBooleanTrue
                 forKey:kCATransactionDisableActions];

[self.view.layer addAnimation:transition forKey:nil];
[self  popViewControllerAnimated:NO];    
[CATransaction commit];
}

您还可以使用UIView的动画为pop创建自定义过渡。在这里,您将动画应用于即将消失的视图,然后从视图堆栈中弹出它:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO]; 

[self.navigationController popViewControllerAnimated:NO];

[UIView commitAnimations];