【问题标题】:iPhone:Animation move view controller upwardiPhone:动画向上移动视图控制器
【发布时间】:2025-12-04 18:45:01
【问题描述】:

我正在开发一个 iPhone 应用程序,我需要在动画中缓慢地向上移动第一个视图控制器并移动到第二个视图控制器。我正在考虑使用 CoreAnimation 将第一个视图控制器缓慢向上移动并推送到下一个视图控制器。有人可以帮助提供可用于实现此目的的类/api吗?

谢谢!

【问题讨论】:

    标签: iphone


    【解决方案1】:

    试试这个,

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0];
        [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:yourfirstview cache:YES];
        [UIView setAnimationDidStopSelector:@selector(remove_view)];
        //change frame here
        yourfirstview.frame =CGRectMake(0,-480,320,480);
        [UIView commitAnimations];
    
       -(void)remove_view
        {
           [yourfirstview removeFromSuperview];
           [UIView beginAnimations:nil context:nil];
           [UIView setAnimationDuration:1.0];
           [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:yoursecondview cache:YES];
           //change frame here
           yoursecondview.frame =CGRectMake(0,0,320,480);
           [UIView commitAnimations];
        }
    

    【讨论】:

    • 嗨,我正在尝试推送到新的视图控制器,而不是 UIView。所以,我想使用 Core Animation(它支持 CATransition 向上移动)并提交动画并使用 pushViewController 移动到下一个屏幕。
    【解决方案2】:

    您可以在单个视图控制器上使用多个UIViews 轻松实现此目的

    根据 Apple 的文档:

    在 iOS 4 及更高版本中,使用基于块的动画方法。

    因此,为了产生预期的结果,请使用以下代码。

    UIView *mySecondView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 480)];
    [mySecondView setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:mySecondView];
    
    [UIView animateWithDuration:2.0 delay:3.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        [mySecondView setTransform:CGAffineTransformMakeTranslation(0, -480)];
    }completion:^(BOOL done){
    
    }];
    

    【讨论】:

    • 嗨,我正在尝试推送到新的视图控制器,而不是 UIView。
    • @user213199 推送视图控制器是指将视图向左移动以在右侧显示新视图。这是使用UINavigationController 完成的