我在使用Xcode的普通Storyboard和Push Segue,但是我想要一种只显示下一个视图而不是滑动下一个视图的Segue(就像当您使用选项卡栏时下一个视图只是出现的)。
有没有一种简单的方法让普通的push segues只是"出现"而不是"滑动",而无需添加自定义的segues?
一切都正常工作,我只是想在视图之间删除那个滑动动画。
我在使用Xcode的普通Storyboard和Push Segue,但是我想要一种只显示下一个视图而不是滑动下一个视图的Segue(就像当您使用选项卡栏时下一个视图只是出现的)。
有没有一种简单的方法让普通的push segues只是"出现"而不是"滑动",而无需添加自定义的segues?
一切都正常工作,我只是想在视图之间删除那个滑动动画。
通过创建自定义segue(基于此链接)我能够做到这一点。
PushNoAnimationSegue
(或者任何你决定称其为的名称)。import UIKit
/*
Move to the next screen without an animation.
*/
class PushNoAnimationSegue: UIStoryboardSegue {
override func perform() {
self.source.navigationController?.pushViewController(self.destination, animated: false)
}
}
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
Ian的答案很好用!
如果有人需要,这是Segue的Swift版本:
已更新至Swift 5,2020年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)
}
}
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];
希望这能帮到其他人!
以下是适用于Swift的版本,用于以模态方式呈现ViewController而不进行动画:
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)
}
}
使用Swift3进行回答 -
对于“push”转场:
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)
}
}
对于使用 Xamarin iOS 的任何人,您的自定义 segue 类需要如下所示:
[Register ("PushNoAnimationSegue")]
public class PushNoAnimationSegue : UIStoryboardSegue
{
public PushNoAnimationSegue(IntPtr handle) : base (handle)
{
}
public override void Perform ()
{
SourceViewController.NavigationController.PushViewController (DestinationViewController, false);
}
}
在Swift中,只需在UINavigationController.pushViewController
上设置animated
false即可。
self.navigationController!.pushViewController(viewController, animated: false)
对我来说,最简单的方法是:
UIView.performWithoutAnimation {
self.performSegueWithIdentifier("yourSegueIdentifier", sender: nil)
}
iOS 7.0及以上版本可用
无动画推送: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)
}
}
}
使用方法:按照图片所示,在Storyboard中设置segue类。在你想要调用performSegue的地方,将isAnimationRequired从你的viewcontroller设为false,这样就可以不带动画地推出segue,然后在调用self.performSegue后将其设回true。祝你好运....
DispatchQueue.main.async {
self.isAnimationRequired = false
self.performSegue(withIdentifier: "showAllOrders", sender: self);
self.isAnimationRequired = true
}