Preface: Custom transition animation is not difficult, the key is to be able to understand the idea, that is, the operation steps. This blog mainly take present transition animation as an example, for analysis, operation, if there is a mistake welcome Jane to communicate with me.
Without modification, presentViewController:animated:completion:
I believe this method has been used by many people, known as the modal launch interface, the default is to launch a new controller from the bottom of the screen.
The purpose of customization is to modify the fixed rollout style, plus the animations you want.
A key concept: UIViewControllerAnimatedTransitioning
control the animation of the Protocol, need to implement this yourself. Here's a look at how the code is implemented.
@interface ViewController ()<UIViewControllerTransitioningDelegate>
Jump click Method
- (void) Click: (UIButton*) sender{Modelviewcontroller *modalviewcontroller = [Modelviewcontroller new];//Designated AgentModalviewcontroller. Transitioningdelegate= Self;//style = Custom ModeModalviewcontroller. Modalpresentationstyle= Uimodalpresentationcustom; [ SelfPresentviewcontroller:modalviewcontroller Animated:YESCompletion:NULL];}
Uiviewcontrollertransitioningdelegate
//Modify the animation of the two related protocol methods, it is recommended to carefully review the comments inside the uiviewcontrollertransitioningdelegate-(ID<UIViewControllerAnimatedTransitioning>) Animationcontrollerforpresentedcontroller: (Uiviewcontroller*) presented Presentingcontroller: (Uiviewcontroller*) Presenting Sourcecontroller: (Uiviewcontroller*) source{//Launch Animation-The following will show the code I wrote with reference to the pop animation demo.return[Presentanimator new];} - (ID<UIViewControllerAnimatedTransitioning>) Animationcontrollerfordismissedcontroller: (Uiviewcontroller*) dismissed{//Exitreturn[Dismissanimator new];}//Want to implement gesture control can be modified in this method-(ID<UIViewControllerInteractiveTransitioning>) Interactioncontrollerfordismissal: (ID<UIViewControllerAnimatedTransitioning>) Animator {}
The key is how the animation realizes the implementation of Presentanimator, Dismissanimator.
UIViewControllerAnimatedTransitioning
Use this protocol for modification.
@interface PresentAnimator : NSObject <UIViewControllerAnimatedTransitioning>
Presentanimator.m
Time spent interacting-(Nstimeinterval) Transitionduration: (ID <UIViewControllerContextTransitioning>) transitioncontext{ Return0.5F;}//specific animation way, mentioned abovePOP-(void) Animatetransition: (ID <UIViewControllerContextTransitioning>) transitioncontext{//Here Get the view of the newly launched VC UIView *toview = [Transitioncontext Viewcontrollerforkey:uitransitioncontexttoviewcontrollerkey]. View;Set exit view here to specify how to set Toview. Frame= CGRectMake (0,0, Screen_width, Screen_height);Toview. Center= Cgpointmake (transitioncontext. Containerview. Center. x,-transitioncontext. Containerview. Center. Y);[Transitioncontext. ContainerviewAddsubview:toview];//PopAnimation popspringanimation *positionanim = [Popspringanimation animationwithpropertynamed:kpoplayerpositiony];Positionanim. Tovalue= @ (Transitioncontext. Containerview. Center. Y);Positionanim. Springbounciness=Ten;[Positionanim setcompletionblock:^ (Popanimation *anim, BOOL finished) {[Transitioncontext Completetransition:yes];}];Popspringanimation *scaleanim = [Popspringanimation animationwithpropertynamed:kpoplayerscalexy];Scaleanim. Springbounciness= -;Scaleanim. Fromvalue= [Nsvalue Valuewithcgpoint:cgpointmake (1.2,1.4)];[Toview. LayerPop_addanimation:positionanim forkey:@"Positionanimation"];[Toview. LayerPop_addanimation:scaleanim forkey:@"Scaleanimation"];}
Dismissanimator.m
- (Nstimeinterval) Transitionduration: (ID<UIViewControllerContextTransitioning>) transitioncontext{return 0.5F;} - (void) Animatetransition: (ID<UIViewControllerContextTransitioning>) transitioncontext{//Fromvc Good to understand, right? is which controller to push over to go back to where.Uiviewcontroller*FROMVC = [Transitioncontext Viewcontrollerforkey:uitransitioncontextfromviewcontrollerkey]; Popbasicanimation *offscreenanimation = [Popbasicanimation animationwithpropertynamed:kpoplayerpositiony]; Offscreenanimation. Tovalue= @ (-FROMVC. View. Layer. Position. Y); [Offscreenanimation setcompletionblock:^ (Popanimation *anim,BOOLFinished) {[Transitioncontext completetransition:YES]; }]; [Fromvc. View. LayerPop_addanimation:offscreenanimation forkey:@"Offscreenanimation"];}
Incidentally, it is also worth mentioning that the animations of uinavigationcontroller transitions can be customized. Through this
UINavigationControllerDelegate
to complete
// 签订这个协议self.navigationController.delegateself;
- (id<UIViewControllerAnimatedTransitioning>) navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewControllerif (operation == UINavigationControllerOperationPush) { // 动画的方式不变return [PresentAnimator new]; }elsereturnnil; }}
IOS Custom Transitions Animation Chapter