90

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

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

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

4

11 回答 11

143

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

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

在 Xcode 中指定 segue 类

斯威夫特 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
于 2013-08-29T11:22:49.937 回答
50

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

在此处输入图像描述

于 2015-10-14T02:39:44.353 回答
35

伊恩的回答效果很好!

如果有人需要,这是 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)
    }
}
于 2014-11-01T20:46:34.643 回答
6

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

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

希望这对其他人有帮助!

于 2013-04-27T13:35:52.017 回答
4

这是适用于模态呈现不带动画的 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)
    }

}
于 2015-05-10T15:31:40.437 回答
2

使用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)
    }
}
于 2017-01-29T09:30:14.553 回答
1

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

于 2015-08-28T14:19:08.700 回答
0

只需在Swift 中设置animated falseUINavigationController.pushViewController

self.navigationController!.pushViewController(viewController, animated: false)
于 2015-12-17T18:58:58.093 回答
0

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

UIView.performWithoutAnimation { 

        self.performSegueWithIdentifier("yourSegueIdentifier", sender: nil)

    }

适用于 iOS 7.0

于 2016-05-13T16:00:52.827 回答
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)
        }
    }
}

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

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

如图所示,从情节提要中设置 segue 类。

于 2018-07-23T18:09:18.783 回答
0

我正在使用带有 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"/>
于 2017-05-25T22:38:34.543 回答