【问题标题】:Transitions between UIView in the same UIViewController同一个 UIViewController 中的 UIView 之间的转换
【发布时间】:2013-07-08 09:58:43
【问题描述】:

当我按下按钮时,我想在 UIViewController 中从 UIView 转换到另一个

我写了这段代码:

- (IBAction)changeView:(id)sender {

    CATransition* transition = [CATransition animation];
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    transition.duration = 1.0f;
    transition.type =  @"cube";
    transition.subtype = @"fromTop";
    [self.view1.layer removeAllAnimations];
    [self.view1.layer addAnimation:transition forKey:kCATransition];


}

此代码会将 view1(UIview) 传输到它自己。我怎样才能让它过渡到另一个 UIView(例如 view2)。我还可以在情节提要中将另一个 UIview (view2) 放在哪里

【问题讨论】:

    标签: iphone objective-c xcode transition uivewcontroller


    【解决方案1】:

    如果您有两个 UIView,例如将 firstViewsecondView 添加到同一矩形上的 UIViewController。并将隐藏属性设置为 secondView。

    -(IBAction)flipFirstViewToSecondView{
    secondView.hidden= NO;
        [UIView transitionWithView:firstView duration:1.0f options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
    
            [UIView transitionWithView:secondView duration:1.0f options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
    
            } completion:^(BOOL finished) {
                secondView.hidden = NO;
                firstView.hidden = YES;
            }];
    
        } completion:^(BOOL finished) {
    
        }];
    }
    

    谢谢。如果您在集成它时遇到任何问题,请告诉我。

    【讨论】:

    • 非常感谢您的帮助,但是当我更改交通选项时,这看起来不像是一个高质量的解决方案。另外,我想使用 CATransition b/c 它有更多的传输选项,例如立方体过境。无论如何谢谢
    【解决方案2】:

    看看这段代码 sn-p 它更优雅。还可以查看 Apple 的文档以了解更多花哨的 UIViewAnimationOptions

    - (IBAction)changeView:(id)sender
    {
        [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
            //Remove View-1 from the main view
            [view1 removeFromSuperView];
            //Add the new view to your current view
            [self.view addSubView:view2];
        } completion:^(BOOL finished) {
            //Once animation is complete
        }];
    }
    

    确保这两个视图是视图控制器的强属性。

    【讨论】:

    • 我在网上看到了很多这段代码,但对我不起作用。当我按下按钮时,仅 view1 隐藏(已删除)!
    【解决方案3】:

    你试过这样吗?

      self.view1.alpha = 0.0;
    
     [UIView beginAnimations:@"flip" context:nil];
    
     [UIView setAnimationDelegate:self];
    
     [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    
     [UIView setAnimationDuration:1.0f];
    
      self.view2.alpha = 1.0;   
    
      [UIView commitAnimations];
    

    【讨论】:

    • 我想使用 CATransition B/C 它有 Cube 传输。 FIY 你的代码对我不起作用:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多