iOS - 创建自定义过渡动画
Posted
技术标签:
【中文标题】iOS - 创建自定义过渡动画【英文标题】:iOS - Creating a custom Transition animation 【发布时间】:2015-04-11 22:03:33 【问题描述】:我正在尝试在视图控制器之间创建自定义转换,这些控制器使用自定义 segues 链接到容器视图控制器。
这是我创建自定义过渡的尝试。然而,所发生的是视图按预期上下动画。但是视图控制器会立即切换。我要创建的是视图垂直滑出屏幕,然后容器切换视图控制器,然后它滑回屏幕。
任何想法如何使用自定义动画来实现此动画?
- (void)swapFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
toViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[fromViewController willMoveToParentViewController:nil];
[self addChildViewController:toViewController];
[self transitionFromViewController:fromViewController toViewController:toViewController duration:.4 options:UIViewAnimationOptionCurveEaseOut animations:^
self.view.frame = CGRectMake(0, 560, self.view.frame.size.width, self.view.frame.size.height);
completion:^(BOOL finished)
[UIView animateWithDuration:.4 delay:0 options:UIViewAnimationOptionCurveLinear animations:^
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
completion:^(BOOL finished)
[fromViewController removeFromParentViewController];
[toViewController didMoveToParentViewController:self];
];
];
【问题讨论】:
【参考方案1】:[UIViewController transitionFromViewController:toViewController]
仅在 fromViewController
和 toViewController
都已经是接收器的子控制器时有效。
transitionFromViewController:toViewController:
的文档说明如下:
视图控制器的两个子视图控制器之间的转换。
fromViewController:一个视图控制器,其视图当前在父视图层次结构中可见。
toViewController:其视图当前不在视图层次结构中的子视图控制器。
但是我没有测试它,我认为这会起作用:
- (void)swapFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
CGFloat width = CGRectGetWidth(self.view.frame);
CGFloat height = CGRectGetHeight(self.view.frame);
[fromViewController willMoveToParentViewController:nil];
[self addChildViewController:toViewController];
toViewController.view.frame = CGRectMake(0.0, height, width, height);
[self.view addSubView:toViewController.view];
[UIView transitionWithView:self.view
duration:0.4
options:UIViewAnimationOptionCurveLinear
animations:^
fromViewController.view.frame = CGRectMake(0.0, height, width, height);
toViewController.view.frame = CGRectMake(0.0, 0.0, width, height);
completion:^(BOOL finished)
[fromViewController removeFromParentViewController];
[fromViewController.view removeFromSuperView];
[toViewController didMoveToParentViewController:self];
];
【讨论】:
以上是关于iOS - 创建自定义过渡动画的主要内容,如果未能解决你的问题,请参考以下文章
UIViewController 动画过渡在 iOS8 中失败