IOS does not use Uinavigationcontroller to implement push animations

Source: Internet
Author: User

From Liu Xuefeng

In iOS development, if you use the Uinavigationcontroller, with the Storyboard+push mode segue, by default, you can directly implement the view switch effect of the left and right.

However, if you do not use Uinavigationcontroller, the segue is set to push, the operation will be directly error, and model mode segue only cover Vertical,flip Horizontal,cross There are 4 modes of dissolve and partial curl, no push mode.

If you want to use push mode in a custom view toggle animation, the only way is to set segue to custom and then customize the push animation.

The custom view toggle animation effect is implemented by deriving a class from Uistoryboardsegue and then -(void)perform animating itself in the method.

After many times Google, find a variety of implementations, after comparison found that the simplest and most reliable push animation implemented as follows:

-(void) perform{uiviewcontroller* Source = (Uiviewcontroller *) self. Sourceviewcontroller;uiviewcontroller* Destination = (Uiviewcontroller *) self. Destinationviewcontroller;CGRect sourceframe = Source. View. Frame;Sourceframe. Origin. x=-sourceframe. Size. Width;CGRect Destframe = Destination. View. Frame;Destframe. Origin. x= Destination. View. Frame. Size. Width;Destination. View. Frame= Destframe;Destframe. Origin. x=0;[Source. View. SuperviewAddsubview:destination. View];[UIView animatewithduration:.animations:^{Source. View. Frame= Sourceframe;Destination. View. Frame= Destframe;} completion:^ (BOOL finished) {[Source presentviewcontroller:destination Anim Ated:no Completion:nil];}];}

The above is the implementation of switching from right to left view animation, the key code is:

sourceFrame.origin.x = -sourceFrame.size.width;

And

destFrame.origin.x = destination.view.frame.size.width;

Move the old view to the left, and the new view starts at the right.
Finally, after the animation is finished, manually switch the current view to the new view:

[source presentViewController:destination animated:NO completion:nil];

Because the animation effect we have already implemented, so switch to the new view do not use the animation.
Simply modify the above code to implement a left-to-right push toggle Animation:

CGRect sourceframe = Source. View. Frame;Sourceframe. Origin. x= Sourceframe. Size. Width;CGRect Destframe = Destination. View. Frame;Destframe. Origin. x=-destination. View. Frame. Size. Width;Destination. View. Frame= Destframe;

Using the custom mode of the segue, you can achieve any switching animation effect, for simple non-3D animation, a few simple lines of code can be achieved.

IOS does not use Uinavigationcontroller to implement push animations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.