繁体   English   中英

iOS-创建自定义过渡动画

[英]iOS - Creating a custom Transition animation

我正在尝试创建一个自定义过渡,以查看与具有自定义segue的Container View Controller链接的视图控制器。

这是我创建自定义过渡的尝试。 但是,发生的是视图按预期上下动画。 但是视图控制器会立即切换。 我要创建的是视图垂直滑离屏幕,然后容器切换视图控制器,然后滑回屏幕。

有什么想法可以通过自定义动画实现此动画吗?

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

[UIViewController transitionFromViewController:toViewController]仅在fromViewControllertoViewController已经是接收者的子控制器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];
                }];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM