【问题标题】:iOs Segue animation left to right (horizontal)iOs Segue 动画从左到右(水平)
【发布时间】:2012-04-07 06:12:30
【问题描述】:

我几乎是 Xcode 4 的新手。 有一种方法可以将自定义过渡动画添加到 segue 中,它不在故事板管理中 Interface Builder 提供的四个动画中? 具体来说,我想要一个类似于正常的“垂直覆盖”但水平的动画。我希望一个视图从左到右(或从右到左)传递到另一个滑动,而不是从上到下,因为它发生在“覆盖垂直”过渡中。 我尝试了滑动手势但没有运气:即使是从上到下的转换,无论如何,我不明白为什么当所有应用程序的默认转换通常是从右到左或左时,默认转换是从上到下向右,尤其是在您滑动的情况下...

我也尝试了一种编程方式,但即使在这种情况下也没有运气,使用以下代码:

#import "JHCustomSegue.h"

@implementation JHCustomSegue
- (void) perform {
  UIViewController *src = (UIViewController *) self.sourceViewController;
  UIViewController *dst = (UIViewController *) self.destinationViewController;

  [UIView transitionWithView:src.navigationController.view duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        [src presentModalViewController:dst animated:NO];
    }
    completion:NULL];
}

@end

在界面生成器中,我将此类定义为我的 segue 类。使用断点,我看到它进入了函数 perfom 但是......不要执行! 我已经以纵向模式阻止了该应用程序(不知道是否有问题)。我试图在真实的 ipad 和模拟的 iphone 上运行该应用程序。同样的问题。

【问题讨论】:

    标签: ios cocoa-touch storyboard transition uistoryboardsegue


    【解决方案1】:

    在 Github 上查看这个示例:

    https://github.com/itinance/iOSCustomSegue

    它在示例应用程序中明确使用从右到左的自定义 segue,而没有 UINavigationController 作为请求的线程开启器。

    Sahil 答案中的代码在 iOS 8 中对我不起作用。所以我不得不对这个问题进行更多调查。

    'UIView animateWithDuration' 在 iOS 8 上工作,而 'destinationController.view.layer addAnimation:transition' 与 presentViewController 结合不做任何事情。

    【讨论】:

      【解决方案2】:

      根据 Zakir Hyder 的提交,这里是翻译成 Swift 的相同功能:

      override func perform() {
      
          var sourceViewController = self.sourceViewController as UIViewController
          var destinationViewController = self.destinationViewController as UIViewController
      
          var transition: CATransition = CATransition()
      
          transition.duration = 0.25
          transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
          transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
          transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
      
          sourceViewController.navigationController?.view.layer.addAnimation(transition, forKey: "kCATransition")
          sourceViewController.navigationController?.pushViewController(destinationViewController, animated: false)     
      
      }
      

      【讨论】:

      • 如果不使用导航控制器,这段代码在 Swift 中会是什么样子?
      【解决方案3】:

      这是不使用导航控制器时自定义序列的工作代码。

      -(void)perform
      {
      
      UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
      UIViewController *destinationController = (UIViewController*)[self destinationViewController];
      
      CATransition* transition = [CATransition animation];
      transition.duration = 0.25;
      transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
      transition.type = kCATransitionMoveIn; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
      transition.subtype = kCATransitionFromRight; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
      
      [destinationController.view.layer addAnimation:transition forKey:kCATransition];
      [sourceViewController presentViewController:destinationController animated:NO completion:nil];
      }
      

      【讨论】:

      • 这在 Swift 中会是什么样子?
      【解决方案4】:

      您可以在自定义 Segue 中使用 CATransition 来实现从左到右的过渡。将此 #import "QuartzCore/QuartzCore.h" 添加到您的自定义 Segue

      -(void)perform {
      
      __block UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
      __block UIViewController *destinationController = (UIViewController*)[self destinationViewController];                    
      
      CATransition* transition = [CATransition animation];
      transition.duration = .25;
      transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
      transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
      transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
      
      
      
      [sourceViewController.navigationController.view.layer addAnimation:transition
                                                  forKey:kCATransition];
      
      [sourceViewController.navigationController pushViewController:destinationController animated:NO];    
      
      
      }
      

      【讨论】:

      • 屏幕会不会从底部往上推?我将这段代码用作自定义 segue,这正是我提到的。
      • 我发现问题出在哪里。它是横向的,就像是纵向的一样。
      • 您可以使用 kCATransitionFromBottom 作为 transition.subtype。看这里developer.apple.com/library/ios/documentation/GraphicsImaging/…
      • 是的,这正是我所做的。无论如何,Jakir,感谢上面的精彩回答,真的很有帮助。
      【解决方案5】:

      我也尝试使用 Segue 来实现相同的效果,但没有成功。相反,我使用了一种解决方法:

      // get the view that's currently showing
      UIView *currentView = self.view;
      // get the the underlying UIWindow, or the view containing the current view
      UIView *theWindow = [currentView superview];
      
      UIView *newView = aTwoViewController.view; 
      
      // remove the current view and replace with myView1
      [currentView removeFromSuperview];
      [theWindow addSubview:newView];
      
      // set up an animation for the transition between the views
      CATransition *animation = [CATransition animation];
      [animation setDuration:0.5];
      [animation setType:kCATransitionPush];
      [animation setSubtype:kCATransitionFromRight];
      [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
      
      [[theWindow layer] addAnimation:animation forKey:@"SwitchToView2"];
      

      您可以下载示例项目here。干杯!

      【讨论】:

      • 我想补充的是,如果您确实找到了使用 Segue 的方法,请告诉我们。 =)
      • 不,直到现在 :) 但是,我认为可以更多地使用导航控制器。我会尽快为此花点时间。
      • 这与@Zakir Hyder 的答案基本相同,但形式不同。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-30
      • 2016-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多