【问题标题】:Push segue in xcode with no animation在没有动画的xcode中推送segue
【发布时间】:2015-10-15 23:50:35
【问题描述】:

我在 xcode 中使用普通的故事板和推送转场,但我希望转场只出现在下一个视图中,而不是滑动下一个视图(就像您使用标签栏并且下一个视图刚刚出现时一样)。

有没有一种简单的方法可以让普通的推送转场只是“出现”而不是“滑动”,而不需要添加自定义转场?

一切正常,我只想删除视图之间的幻灯片动画。

【问题讨论】:

  • 我刚刚尝试将推送转场更改为模态转场,因为这可以让我删除动画,但我有一个带有顶部工具栏的表格视图,将转场设置为模态会删除此顶部栏,我找不到任何方法可以重新添加顶部栏!所以我需要一个不会为过渡设置动画但不会破坏我的 tableview 的解决方案。

标签: xcode animation segue


【解决方案1】:

伊恩的回答很好!

这是 Segue 的 Swift 版本,如果有人需要:

2020 年 5 月 SWIFT 5 更新

PushNoAnimationSegue.swift

import UIKit

/// Move to the next screen without an animation
class PushNoAnimationSegue: UIStoryboardSegue {
override func perform() {
    if let navigation = source.navigationController {
        navigation.pushViewController(destination as UIViewController, animated: false)
    }
}

【讨论】:

  • 可能由于 Swift 版本,我需要“as!”而不是你使用它们的两个地方的“as”。
  • 嗯,也许在 Swift 1.2 中?我在 Xcode 7 Beta1 中检查了 Swift 2.0,它编译时没有任何警告。
【解决方案2】:

无动画推送:Swift这对我有用。

import ObjectiveC

private var AssociatedObjectHandle: UInt8 = 0
    extension UIViewController {

        var isAnimationRequired:Bool {
            get {
        return (objc_getAssociatedObject(self, &AssociatedObjectHandle) as? Bool) ?? true
            }
            set {
                objc_setAssociatedObject(self, &AssociatedObjectHandle, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            }
        }
    }

    -------------------- SilencePushSegue --------------------

class SilencePushSegue: UIStoryboardSegue {

    override func perform() {
        if self.source.isAnimationRequired == false {
            self.source.navigationController?.pushViewController(self.destination, animated: false)
        }else{
            self.source.navigationController?.pushViewController(self.destination, animated: true)
        }
    }
}

用法 : 如图所示从情节提要中设置 segue 类。当你想在没有动画的情况下推送 segue 并在调用 self.performSegue 后将其设置回 true 时,将 viewcontroller 中的 isAnimationRequired 设置为 false,从你想调用 performSegue 的位置。祝你好运......

DispatchQueue.main.async {
                self.isAnimationRequired = false
                self.performSegue(withIdentifier: "showAllOrders", sender: self);
                self.isAnimationRequired = true
            }

【讨论】:

    【解决方案3】:

    我可以通过创建自定义 segue(基于 this link)来做到这一点。

    1. 创建一个新的 segue 类(见下文)。
    2. 打开 Storyboard 并选择 segue。
    3. 将类设置为 PushNoAnimationSegue(或您决定命名的任何名称)。

    斯威夫特 4

    import UIKit
    
    /*
     Move to the next screen without an animation.
     */
    class PushNoAnimationSegue: UIStoryboardSegue {
    
        override func perform() {
            self.source.navigationController?.pushViewController(self.destination, animated: false)
        }
    }
    

    目标 C

    PushNoAnimationSegue.h

    #import <UIKit/UIKit.h>
    
    /*
     Move to the next screen without an animation.
     */
    @interface PushNoAnimationSegue : UIStoryboardSegue
    
    @end
    

    PushNoAnimationSegue.m

    #import "PushNoAnimationSegue.h"
    
    @implementation PushNoAnimationSegue
    
    - (void)perform {
    
        [self.sourceViewController.navigationController pushViewController:self.destinationViewController animated:NO];
    }
    
    @end
    

    【讨论】:

    • 感谢您的评论和意见。
    • 最佳答案。有什么想法可以为背景动画实现同样的效果吗?
    • 像魅力一样工作。这也是故事板转场的“正确”方式。
    • 有没有人为上面的答案停止了后面的动画。
    • 不是解决方案,自定义 segue 呈现在整个布局上,就像模态推送一样,而原始推送 segue 尊重 window.frame navigationController tabBarController 等...如何配置 segue 使其表现得像推动转场而不是像模态转场?或者... $1.000.000 的问题,如何在使用 STORYBOARDS 配置常规 segue 时指定 animations=NO
    【解决方案4】:

    我正在使用带有 Xamarin 的 Visual Studio,而设计器没有在 dtochetto 的回答中提供“动画”复选标记。

    请注意,XCode 设计器会将以下属性应用于 .storyboard 文件中的 segue 元素:animates="NO"

    我手动编辑了 .storyboard 文件并将 animates="NO" 添加到 segue 元素,它对我有用。

    例子:

     <segue id="1234" destination="ZB0-vP-ctU" kind="modal" modalTransitionStyle="crossDissolve" animates="NO" identifier="screen1ToScreen2"/>
    

    【讨论】:

      【解决方案5】:

      使用 Swift3 回答 -

      对于“推动”转场:

      class PushNoAnimationSegue: UIStoryboardSegue
      {
          override func perform()
          {
             source.navigationController?.pushViewController(destination, animated: false)
          }
      }
      

      对于“模态”转场:

      class ModalNoAnimationSegue: UIStoryboardSegue
      {
          override func perform() {
              self.source.present(destination, animated: false, completion: nil)
          }
      }
      

      【讨论】:

        【解决方案6】:

        对我来说,最简单的方法是:

        UIView.performWithoutAnimation { 
        
                self.performSegueWithIdentifier("yourSegueIdentifier", sender: nil)
        
            }
        

        适用于 iOS 7.0

        【讨论】:

        • 如果使用自适应转场,我相信虽然 performWithoutAnimation 设置了动画禁用,但在转场执行之前,如果转场检查了“动画”,动画会再次启用。执行后,如果以前是,它会再次禁用它们。
        • 但是您可以通过创建 UIStoryboardSegue 子类并在执行之前再次禁用动画来解决它(例如,使用构造函数检查它们是否被禁用并将其存储在属性中)。
        【解决方案7】:

        只需在 Swift 中将 animated false 设置为 UINavigationController.pushViewController

        self.navigationController!.pushViewController(viewController, animated: false)
        

        【讨论】:

        • 不知道为什么这被否决了。非常适合我!
        【解决方案8】:

        您可以在 iOS 9 的 Interface Builder 中取消选中“动画”

        【讨论】:

        • 这是原始问题的答案!其他答案显示了如何在 objc/swift 代码中实现这一点,而 dtochetto 仅使用 Xcode(如原始问题中所要求的)。
        • 然而,对于放松转场,它似乎不起作用
        • 这是最好和最快的解决方案,不幸的是这不适用于 iOS 8。
        • @DanielT。也许,您可以在情节提要上拖动 2 个 segue,一个有动画,另一个没有动画。给他们不同的Identifier名字。
        • 请注意,XCode 只是为 .storyboard 文件中的 segue 元素添加了一个属性。您可以手动编辑 .storyboard 文件并将 animates="NO" 添加到您关心的 segues 如果您使用的是 Visual Studio 并且设计器中没有复选标记。
        【解决方案9】:

        对于使用 Xamarin iOS 的任何人,您的自定义 segue 类需要如下所示:

        [Register ("PushNoAnimationSegue")]
        public class PushNoAnimationSegue : UIStoryboardSegue
        {
            public PushNoAnimationSegue(IntPtr handle) : base (handle)
            {
        
            }
        
            public override void Perform ()
            {
                SourceViewController.NavigationController.PushViewController (DestinationViewController, false);
            }
        }
        

        不要忘记,您仍然需要在故事板中设置自定义 segue,并将类设置为 PushNoAnimationSegue 类。

        【讨论】:

          【解决方案10】:

          这是适应模态呈现不带动画的 ViewController 的 Swift 版本:

          import UIKit
          
          /// Present the next screen without an animation.
          class ModalNoAnimationSegue: UIStoryboardSegue {
          
              override func perform() {
                  self.sourceViewController.presentViewController(
                      self.destinationViewController as! UIViewController,
                      animated: false,
                      completion: nil)
              }
          
          }
          

          【讨论】:

          • 这段代码是从@zavié 的答案中复制而来的,没有添加任何东西,但确实更糟:不检查包含的选项
          • 这与@zavié 的回答不同,因为它显示了如何模态 呈现没有动画的 ViewController。很高兴将其作为参考,因为它与问题有些相关。
          【解决方案11】:

          我现在已经设法使用以下代码做到这一点:

          CreditsViewController *creditspage = [self.storyboard instantiateViewControllerWithIdentifier:@"Credits"];
          [UIView beginAnimations:@"flipping view" context:nil];
          [UIView setAnimationDuration:0.75];
          [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:YES];
          [self.navigationController pushViewController:creditspage animated:NO];
          [UIView commitAnimations];
          

          希望这对其他人有帮助!

          【讨论】:

          • 您可以将其添加到 UIButton 或其他的操作中。如果要跳转到没有动画的新视图控制器,可以使用: - void(yourUIButtonAction) { CreditsViewController *creditspage = [self.storyboard instantiateViewControllerWithIdentifier:@"Credits"]; [self.navigationController pushViewController:creditspage Animation:NO]; }
          • 这甚至没有回答问题。你这里没有使用 segue !
          猜你喜欢
          • 1970-01-01
          • 2012-01-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-03
          • 1970-01-01
          相关资源
          最近更新 更多