customizing modal animations

Source: Internet
Author: User

In many scenarios, we all need to implement a variety of animations, this time we try to engage in the modal animation between the controller jumps.

 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    ZYSecondViewController *second = [[ZYSecondViewController alloc]init];    second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;    [self presentViewController:second animated:YES completion:nil];}
    • The above is the system-provided animation style, but the system provides the animation sometimes can not meet our needs, so we have to customize the animation, we will then focus on the custom animation this piece of content.

    • Preparation: I've written a single example: A line of code to make an order example

    • And the extension of some coordinates, here we call directly:

    • ZYtransition.h (single case)

#import "Singleton.h"@interface ZYtransition : NSObject <UIViewControllerTransitioningDelegate>SingletonH(transition)@end
    • Zytransition.m
#import "ZYtransition.h" #import "ZYAnimatedTransitioning.h" #import "ZYPresentationController.h"  @implementation zytransition SINGLETONM (Transition)-(Uipresentationcontroller *) Presentationcontrollerforpresentedviewcontroller: (Uiviewcontroller*) presented Presentingviewcontroller: (Uiviewcontroller*) Presenting Sourceviewcontroller: (Uiviewcontroller*) source{Zypresentationcontroller *pc = [[Zypresentationcontroller alloc]initwithpresentedviewcontroller:presented Presentingviewcontroller:presenting];returnPC;} -(NullableID<UIViewControllerAnimatedTransitioning>) Animationcontrollerforpresentedcontroller: (Uiviewcontroller*) presented Presentingcontroller: (Uiviewcontroller*) Presenting Sourcecontroller: (Uiviewcontroller*) source{zyanimatedtransitioning *anim = [[zyanimatedtransitioning alloc]init]; Anim. Presented=YES;returnAnim;} -(NullableID<UIViewControllerAnimatedTransitioning>) Animationcontrollerfordismissedcontroller: (Uiviewcontroller*) dismissed{zyanimatedtransitioning *anim = [[zyanimatedtransitioning alloc]init]; Anim. Presented=NO;returnAnim;}
    • ZYAnimatedTransitioning.h (responsible for animation)
@interface ZYAnimatedTransitioning : NSObject<UIViewControllerAnimatedTransitioning>@property(nonatomic,assign)BOOL presented;@end
    • Zyanimatedtransitioning.m
Const NstimeintervalDuraton =2.W; @implementation zyanimatedtransitioning - (Nstimeinterval) Transitionduration: (NullableID<UIViewControllerContextTransitioning>) transitioncontext{returnDuraton;} - (void) Animatetransition: (ID<UIViewControllerContextTransitioning>) transitioncontext{//Uitransitioncontexttoviewkey    //Uitransitioncontextfromviewkey    if( Self. Presented)    {UIView*toview = [Transitioncontext Viewforkey:uitransitioncontexttoviewkey]; Toview. Y=-toview. Height; [UIViewAnimatewithduration:duraton animations:^{Toview. Y=0; } completion:^ (BOOLFinished) {[Transitioncontext completetransition:YES];    }]; }Else{UIView*fromview = [Transitioncontext Viewforkey:uitransitioncontextfromviewkey]; [UIViewAnimatewithduration:duraton animations:^{Fromview. Y=-fromview. Height; } completion:^ (BOOLFinished) {[Transitioncontext completetransition:YES];    }]; }}@end
    • Get a zypresentationcontroller.
      . h
@interface ZYPresentationController : UIPresentationController

. m

 @implementation zypresentationcontroller - (void) presentationtransitionwillbegin{ Self. Presentedview. Frame= Self. Containerview. Bounds; [ Self. ContainerviewAddsubview: Self. Presentedview];} - (void) Presentationtransitiondidend: (BOOL) completed{//NSLog (@ "%s", __func__);}- (void) dismissaltransitionwillbegin{//NSLog (@ "%s", __func__);}- (void) Dismissaltransitiondidend: (BOOL) completed{[ Self. PresentedviewRemovefromsuperview];}@end

In this way, it is very simple for us to use it outside:

#import "ViewController.h" #import "ZYSecondViewController.h" #import "ZYtransition.h"  @interface viewcontroller ()@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload];} - (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{Zysecondviewcontroller *second = [[ZYSecon]    Dviewcontroller Alloc]init]; Second. Modalpresentationstyle= Uimodalpresentationcustom; Second. Transitioningdelegate= [Zytransition sharedtransition]; [ SelfPresentviewcontroller:second Animated:YESCompletion:Nil];}@end

The outside only needs to be the same as usual, then sets the display style to be custom, then formulates the proxy, can realize the modal the custom animation effect.

customizing modal 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.