首页 文章

在没有动画的情况下在xcode中推送segue

提问于
浏览
88

我正在使用正常的故事板并在xcode中推送segues,但是我想要让segues只出现在下一个视图中,而不是滑动下一个视图(就像当你使用标签栏而下一个视图出现时) .

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

一切都工作得很好,我只想删除视图之间的幻灯片动画 .

11 回答

  • 6

    我能够通过创建自定义segue(基于this link)来完成此操作 .

    • 创建一个新的segue类(见下文) .

    • 打开故事板并选择segue .

    • 将类设置为 PushNoAnimationSegue (或您决定调用它的任何内容) .

    Specify segue class in Xcode

    斯威夫特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
    
  • 3

    您可以在Interface Builder for iOS 9中取消选中“Animates”

    enter image description here

  • 46

    伊恩的回答非常好!

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

    PushNoAnimationSegue.swift

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

    我现在设法使用以下代码执行此操作:

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

    希望这有助于其他人!

  • 35

    这是适用于 modally present 没有动画的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)
        }
    
    }
    
  • 2

    使用 Swift3 回答 -

    为“推”segue:

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

    对于“模态”segue:

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

    对于使用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类 .

  • 1

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

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

    可从iOS 7.0获得

  • 0

    我正在使用Visual Studio w / Xamarin,设计师没有在dtochetto的答案中提供“Animates”复选标记 .

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

    我手动编辑了.storyboard文件,并在segue元素中添加了animates =“NO”,这对我有用 .

    例:

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

    PUSH WITHOUT ANIMATION : 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)
            }
        }
    }
    

    Usage :从故事板中设置segue类,如图所示 . 当你想要在没有动画的情况下推送segue并在调用self.performSegue之后将其设置回true时,将viewController中的isAnimationRequired设置为false,从而调用performSegue . 祝你好运....

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

    set the segue class from storyboard as shown in picture.

  • 140

    只需在Swift中的 UINavigationController.pushViewController 上设置 animated false 即可

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

相关问题