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
#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