How to push ViewController with slide effect from left to right? Animation name required

依然范特西╮ 提交于 2019-12-10 19:18:52

问题


EDIT:

  • What is the animation name of the gif below (from right to left)?
  • What is the animation name of the complementary animation of the gif below (from left to right)?

NOTE: I don't want to push a segue. I want to push a view with that animation.


EDIT 2:

Some people are confused when I talk about animation names. However here is an example of an animation name that works (name is: UIViewAnimationTransitionFlipFromRight):

    [UIView  beginAnimations: @"Showinfo"context: nil];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.75];
    [self.navigationController pushViewController: view animated:NO];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
    [UIView commitAnimations];

I am using push and pop to recall the actions on a stack. I believe that Navigation controller keeps the navigation in a stack style structure.


I am using a Navigation controller and I like the effect that I get when I go back (pop) to the previous view using the navigation bar item. It is a nice and smooth from right to left effect.

This is an example of a from right to left effect:

How can I create the complementary a from right to left effect? In other words when I go down in the view hierarchy?

This is how I push the view controller at the moment:

[self.navigationController pushViewController:view animated:YES];

回答1:


We need to add CATransition animation for our viewController. look at this,

    add <CAAnimationDelegate> in your .h file

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    ViewController *VC = [storyboard instantiateViewControllerWithIdentifier:@ViewController"];
    CATransition *transition = [CATransition animation];
    transition.duration = 0.45;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;
    transition.delegate = self;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    [self.navigationController pushViewController:VC animated:NO];



回答2:


Animation name... Well, there is no unique way of calling an animation, but let's try

  • Right to Left slide in (view controller sliding is presented)
  • Left to Right slide out (view controller sliding is dismissed)

There are many examples of UIViewController custom transitions out here, see for example : the github project that re-created sample code for WWDC Session 218: Custom Transitions Using View Controllers (the class of interest to you should be SOLSlideTransitionAnimator, which enables all kinds of 'slide' animations )

EDIT

Ok, you're not talking about animations names, but you're mixing 2 APIs, that are somehow related :

  • UIView animtions (by the way, you're using the OLD API, newer API with Blocks is preferred )

From Apple UIView documentation

Use of this method is discouraged in iOS 4.0 and later. You should use the block-based animation methods to specify your animations instead.

  • UIViewController transitions (like navigation push...)

Now, you're embedding a UINavigationController push call INSIDE a UIView animation transaction. This is WRONG !

Why ? Well, calling UINavigationController pushViewController:animated results in a UIView animation being called - either Apple defaults animations, or you own animation if you provide a UIViewControllerTransitioningDelegate to your controller So basically you're calling an animation inside an animation...

Apple has provided way to customize animations since iOS 7, and you should use that API they provide. This is the API the github sample code I linked to uses. If you need further introduction, read Apple documentation, or other resources




回答3:


For Push you are using already correct way by pushing view, but their is no need of any animation code from your side, only below code is sufficient.

[self.navigationController pushViewController: view animated:NO];

For pop you can add below code in DetailView, which is available from iOS 7.

self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;


来源:https://stackoverflow.com/questions/30374995/how-to-push-viewcontroller-with-slide-effect-from-left-to-right-animation-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!