问题描述
我花了一些时间尝试整理iOS 7中提供的自定义过渡动画。根据, UINavigationControllerDelegate
是要走的路。
I've been spending some time trying to sort out custom transition animations available in iOS 7. According to this question, UINavigationControllerDelegate
is the way to go.
但是,没有示例或文档描述如何最好地在iOS7中进行简单的垂直过渡。 (有一个的其他过渡样式,但没有一个像上下滑动视图一样简单 - 还有一些建议只修改视图位置,但似乎像一个俗气的黑客?)。还有,但他们显然没有使用 UINavigationControllerDelegate
。
There are, however, no examples or documentation describing how best to approach a simple vertical transition in iOS7. (There are a plethora of other transition styles using UINavigationControllerDelegate
, but none as simple as sliding the view up and down -- there are others that suggest just modifying the view position, but that seems like a tacky hack?). There are yet others too that go as far back to 2011, but they're obviously not using UINavigationControllerDelegate
.
免责声明:我很乐意提供代码,但由于还没有任何代码可以发布...但我创建了一个简单的例子,使用jQuery显示我的内容我试图实现。
Disclaimer: I would love to provide code, but since there isn't any code yet to post... I did however create a simple example using jQuery that shows what I'm trying to achieve.
查看小提琴
推荐答案
不,这不是一个俗气的黑客。那就是你做的。自定义过渡动画只是意味着您负责将新视图带入场景 - 只要它最终位于正确的位置即可。因此,从底部对其进行动画处理的方法只是将其放置在底部并将其动画到位。
No, it's not a tacky hack. That's what you do. A custom transition animation simply means that you are in charge of bringing the new view into the scene - provided it ends up in the right place. So the way to animate it in from the bottom is simply to position it at the bottom and animate it up into place.
所以例如(几乎直接来自我书中的示例代码):
So for example (taken almost directly from the example code in my book):
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
// boilerplate
UIViewController* vc1 =
[transitionContext
viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController* vc2 =
[transitionContext
viewControllerForKey:UITransitionContextToViewControllerKey];
UIView* con = [transitionContext containerView];
CGRect r1start = [transitionContext initialFrameForViewController:vc1];
CGRect r2end = [transitionContext finalFrameForViewController:vc2];
UIView* v1 = vc1.view;
UIView* v2 = vc2.view;
// end boilerplate
CGRect r = r2end;
r.origin.y += r.size.height; // start at the bottom...
v2.frame = r;
[con addSubview:v2];
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[UIView animateWithDuration:0.4 animations:^{
v2.frame = r2end; // ... and move up into place
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}];
}
该代码改编自我的示例这个例子几乎就是你所描述的,除了它是一个标签控制器而不是导航控制器,它来自侧面而不是底部。但原则完全相同。
That code is adapted from my example at https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch06p292customAnimation1/ch19p620customAnimation1/AppDelegate.m The example is almost exactly what you are describing except it's for a tab controller instead of a navigation controller and it comes in from the side instead of the bottom. But the principle is exactly the same.
这篇关于在iOS7上使用UINavigationControllerDelegate实现上滑/下滑过渡的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!