2

我正在尝试为 UINavigationController 创建一个类别,以便可以将一些自定义动画放在一起。我遇到了这个问题,它让我开始了。我提出了以下推送功能:

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

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

4

1 回答 1

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 的动画为弹出创建自定义过渡。在这里,您将动画应用于即将消失的视图,然后将其从视图堆栈中弹出:

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

[self.navigationController popViewControllerAnimated:NO];

[UIView commitAnimations];
于 2011-06-21T20:01:55.520 回答