iPhone-UINavigationController-使用CATransaction的自定义动画 [英] iPhone - UINavigationController - custom animation with CATransaction

查看:111
本文介绍了iPhone-UINavigationController-使用CATransaction的自定义动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a category for UINavigationController so I can put together a few custom animations. I came across this question and it got me started. I've come up with the following for a push function:

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

我不知道通过子视图调用正在访问哪个视图/图层。因此,如果有人可以帮助我理解这一点,那将有所帮助。

I don't understand which view/layer is being accessed via the subview call. So if someone could help me understand that, that would be helpful. It seems to be effecting whatever view is about to come onto the screen.

但是我的主要问题是我正在尝试创建一个将撤消上述功能。为清楚起见,假设我有两个视图A和B。A是屏幕上显示的第一个视图。上面的功能将在不按A的情况下移入B。就像模态视图一样。但是下面的函数将A滑回B上,而不是将B滑上而使A留在原处。

But my main question is that I'm trying to create a pop that will "undo" the above function. For clarity sake, lets say I have two views, A and B. A is the first view that is shown on the screen. The function above, will move in B without pushing A out. Much like a modal view would. But the function below slides A back in over B, rather that sliding B up and leaving A in place.

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

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

I don't want to use a modal view and I would rather not use custom animation blocks to change the frame of the views.

推荐答案

我已经完成了类似的工作,尽管我并没有为将其移至其自身的功能中:

I've accomplished something similar, though I didn't bother to move it into it's own function:

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]

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

You could abstract it out. Using your code, it would look like:

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

You can also create a custom transition for pop using UIView's animations. Here you are applying an animation to the view that's about to disappear and then popping it from the view stack:

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

[self.navigationController popViewControllerAnimated:NO];

[UIView commitAnimations];

这篇关于iPhone-UINavigationController-使用CATransaction的自定义动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆