在没有动画的情况下在 xcode 中推送 segue
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16209113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Push segue in xcode with no animation
提问by Richard
I am using normal storyboarding and push segues in xcode, but I want to have segues that just appear the next view, not slide the next view (as in when you use a tab bar and the next view just appears).
我正在使用普通的故事板并在 xcode 中推送 segues,但我希望 segues 只出现在下一个视图中,而不是滑动下一个视图(就像当您使用标签栏并且下一个视图刚刚出现时一样)。
Is there a nice simple way to have normal push segues just "appear" and not "slide", without needing to add custom segues?
有没有一种很好的简单方法可以让普通的推送转场“出现”而不是“滑动”,而无需添加自定义转场?
Everything is working completely fine, I just want to remove that slide animation between the views.
一切正常,我只想删除视图之间的幻灯片动画。
采纳答案by Richard
I have now managed to do this using the following code:
我现在已经使用以下代码设法做到了这一点:
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];
Hope this helps someone else!
希望这对其他人有帮助!
回答by Ian
I was able to do this by creating a custom segue (based on this link).
- Create a new segue class (see below).
- Open your Storyboard and select the segue.
- Set the class to
PushNoAnimationSegue
(or whatever you decided to call it).
- 创建一个新的 segue 类(见下文)。
- 打开您的 Storyboard 并选择 segue。
- 将类设置为
PushNoAnimationSegue
(或您决定将其称为什么)。
Swift 4
斯威夫特 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)
}
}
Objective C
目标 C
PushNoAnimationSegue.h
PushNoAnimationSegue.h
#import <UIKit/UIKit.h>
/*
Move to the next screen without an animation.
*/
@interface PushNoAnimationSegue : UIStoryboardSegue
@end
PushNoAnimationSegue.m
PushNoAnimationSegue.m
#import "PushNoAnimationSegue.h"
@implementation PushNoAnimationSegue
- (void)perform {
[self.sourceViewController.navigationController pushViewController:self.destinationViewController animated:NO];
}
@end
回答by dtochetto
回答by zavié
Ian's answer works great!
伊恩的回答很好用!
Here's a Swift version of the Segue, if anyone needs:
如果有人需要,这是 Segue 的 Swift 版本:
UPDATED FOR SWIFT 5, MAY 2020
2020 年 5 月 Swift 5 更新
PushNoAnimationSegue.swift
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)
}
}
回答by Daniel McLean
Here's the Swift version adapted to modally presenta ViewController without animation:
这是适用于模态呈现没有动画的 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)
}
}
回答by elkorb
answer using Swift3-
使用Swift3回答-
for "push" segue:
对于“推”segue:
class PushNoAnimationSegue: UIStoryboardSegue
{
override func perform()
{
source.navigationController?.pushViewController(destination, animated: false)
}
}
for "modal" segue:
对于“模态”segue:
class ModalNoAnimationSegue: UIStoryboardSegue
{
override func perform() {
self.source.present(destination, animated: false, completion: nil)
}
}
回答by JacobK
For anyone using Xamarin iOS your custom segue class needs to look like this:
对于使用 Xamarin iOS 的任何人,您的自定义 segue 类需要如下所示:
[Register ("PushNoAnimationSegue")]
public class PushNoAnimationSegue : UIStoryboardSegue
{
public PushNoAnimationSegue(IntPtr handle) : base (handle)
{
}
public override void Perform ()
{
SourceViewController.NavigationController.PushViewController (DestinationViewController, false);
}
}
Don't forget you still need set a custom segue in your story board and set the class to the PushNoAnimationSegue class.
不要忘记您仍然需要在故事板中设置自定义转场并将类设置为 PushNoAnimationSegue 类。
回答by Wes
I'm using Visual Studio w/ Xamarin, and the designer doesn't provide the "Animates" checkmark in dtochetto's answer.
我正在使用带有 Xamarin 的 Visual Studio,并且设计者没有在 dtochetto 的回答中提供“动画”复选标记。
Note that the XCode designer will apply the following attribute to the segue element in the .storyboard file: animates="NO"
请注意,XCode 设计器会将以下属性应用于 .storyboard 文件中的 segue 元素:animates="NO"
I manually edited the .storyboard file and added animates="NO" to the segue element(s), and it worked for me.
我手动编辑了 .storyboard 文件并将 animates="NO" 添加到 segue 元素,它对我有用。
Example:
例子:
<segue id="1234" destination="ZB0-vP-ctU" kind="modal" modalTransitionStyle="crossDissolve" animates="NO" identifier="screen1ToScreen2"/>
回答by Usman
PUSH WITHOUT ANIMATION : SwiftHere is what worked for me.
无动画推送: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: Set the segue class from storyboard as shown in picture. set the isAnimationRequired from your viewcontroller to false from where you want to call performSegue, when you want to push segue without animation and set back to true after calling self.performSegue. Best of luck....
用法:如图所示从故事板设置转场类。当您想在没有动画的情况下推送 segue 并在调用 self.performSegue 后将其设置回 true 时,将视图控制器中的 isAnimationRequired 设置为 false,从您想调用 performSegue 的位置开始。祝你好运....
DispatchQueue.main.async {
self.isAnimationRequired = false
self.performSegue(withIdentifier: "showAllOrders", sender: self);
self.isAnimationRequired = true
}
回答by LironXYZ
For me, the easiest way to do so is :
对我来说,最简单的方法是:
UIView.performWithoutAnimation {
self.performSegueWithIdentifier("yourSegueIdentifier", sender: nil)
}
Available from iOS 7.0
从 iOS 7.0 可用