如何在 PopViewController 上改变 ViewController 的动画方向

Posted

技术标签:

【中文标题】如何在 PopViewController 上改变 ViewController 的动画方向【英文标题】:How to change the animation direction of ViewController on PopViewController 【发布时间】:2014-02-10 05:42:26 【问题描述】:

是否可以改变 ViewContoller Pop 动画的方向。 现在,当我们按下 VC 时,它会显示动画 Slide Left-to-Right 和 Pop Right-to-Left。 但我想要在 Pop VC 上从左到右滑动动画。

我尝试使用UIViewAnimationTransition

[UIView  beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
[UIView commitAnimations];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:0.375];
[self.navigationController popViewControllerAnimated:NO];
[UIView commitAnimations];

但它没有我需要的从左到右滑动的动画。这是我得到的动画

typedef enum 
   UIViewAnimationTransitionNone,
   UIViewAnimationTransitionFlipFromLeft,
   UIViewAnimationTransitionFlipFromRight,
   UIViewAnimationTransitionCurlUp,
   UIViewAnimationTransitionCurlDown,
 UIViewAnimationTransition;

【问题讨论】:

尝试:- 首先显示动画并在动画完成时弹出 Viewcontroller。 您找到解决方案了吗? @Crashalot ,我已经为我使用的代码添加了答案,请检查。 【参考方案1】:

干杯,这里是 swift 3.0 ~ 4.0 版本。

let transition = CATransition()
transition.duration = 0.4
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
self.navigationController?.view.layer.add(transition, forKey: kCATransition)
self.navigationController?.popViewController(animated: false)

【讨论】:

【参考方案2】:

我使用了以下代码..

将此添加到didFinishLaunchingWithOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    // Override point for customization after application launch.
    //This line of code needed so you will not see the back window when its pop, match it with your app design.
    self.window.backgroundColor = [UIColor whiteColor];
    return YES;

在您的 POP 代码中添加此代码,

CATransition* transition = [CATransition animation];
transition.duration = 0.4f;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[self.navigationController.view.layer addAnimation:transition
                                            forKey:kCATransition];
[self.navigationController popViewControllerAnimated:NO];

查看此 GIF 中的结果,http://www.giphy.com/gifs/l2YSeBELE1phMEd5S

【讨论】:

【参考方案3】:

您可以使用[UIView beginAnimations:] 处理您的情况。 setTransitionStyle 根据您的需要。检查苹果文档中的可用UIViewAnimationTransitionStyle 参考此https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html

【讨论】:

感谢您的回复。实际上我已经尝试过了,但它没有我需要的从左到右滑动的动画。 我使用了这个***.com/questions/2942221/… 代码,它很好,但不包含我需要的动画。【参考方案4】:
let story = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController")  as! SecondViewController

    UIView.beginAnimations("", context: nil)
    UIView.setAnimationDuration(1.0)
    UIView.setAnimationCurve(UIViewAnimationCurve.easeInOut)
    UIView.setAnimationTransition(UIViewAnimationTransition.flipFromRight, for: (self.navigationController?.view)!, cache: false)
    self.navigationController?.pushViewController(story, animated: true)
    UIView.commitAnimations()

【讨论】:

【参考方案5】:

你可以在动画之后使用完成块来完成这项工作:

UIView.animate(withDuration: 0.75, animations:  () -> Void in

     UIView.setAnimationCurve(UIViewAnimationCurve.easeInOut)
     self.navigationController?.popViewController(animated: true)

)

【讨论】:

【参考方案6】:
 [UIView animateWithDuration:0.5f animations:^
        self.myButton.transform = CGAffineTransformMakeScale(3, 3);
     completion:^(BOOL finished)];
    // for zoom out
    [UIView animateWithDuration:0.5f animations:^

        self.myButton.transform = CGAffineTransformMakeScale(1, 1);
    completion:^(BOOL finished)];
    //View ANimation
    [UIView transitionWithView: self.navigationController.view
                      duration: 0.5
                       options: UIViewAnimationOptionTransitionCurlDown
                    animations: ^

                        [self.navigationController popToRootViewControllerAnimated:YES];
                    
                    completion: nil ];

【讨论】:

以上是关于如何在 PopViewController 上改变 ViewController 的动画方向的主要内容,如果未能解决你的问题,请参考以下文章

如何在没有警告和错误的闭包内使用 navigationController?.popViewController(animated: true)?

为交互式 popviewcontroller guesture 展开 segue

popviewcontroller 没有调用 viewWillappear

popViewController 不会删除 UINavigationBar 项目

PopViewController 没有效果

SwiftUI - SwiftUI 中是不是有等效的 popViewController?