IOS uses Uipresentationcontroller to customize transition animations

Source: Internet
Author: User

1. The default modal animation effect is crawled out of the bottom of the screen.

-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{

SECONDVC *second = [[SECONDVC alloc] init];

[Self Presentviewcontroller:second animated:yes completion:nil];

}

2. To customize the transition animation, first set theDisplay StyleAndTransition Agent

SECONDVC *second = [[SECONDVC alloc] init];

Second.modalpresentationstyle = Uimodalpresentationcustom;

Second.transitioningdelegate = self;

[Self Presentviewcontroller:second animated:yes completion:nil];

3. Implement the transition agent method and tell the program "which object Management controller is present"

-(Uipresentationcontroller *) Presentationcontrollerforpresentedviewcontroller: (Uiviewcontroller *) presented Presentingviewcontroller: (Uiviewcontroller *) presenting Sourceviewcontroller: (Uiviewcontroller *) source

{

return [[Mypresentationcontroller alloc]initwithpresentedviewcontroller:presented Presentingviewcontroller: Presenting];

}

4. Custom Uipresentationcontroller

MYPresentationController.h

@interface Mypresentationcontroller:uipresentationcontroller

@end

Mypresentationcontroller.m

#import "MYPresentationController.h"

@implementation Mypresentationcontroller

-(CGRect) Frameofpresentedviewincontainerview

{

Return CGRectMake (100, 100, 150, 300); Set modal out of the controller frame

}

Here are a few ways to monitor the various states of the modal controller

-(void) Presentationtransitionwillbegin

{

NSLog (@ "Presentationtransitionwillbegin");

}

-(void) Presentationtransitiondidend: (BOOL) completed

{

NSLog (@ "Presentationtransitiondidend");

}

-(void) Dismissaltransitionwillbegin

{

NSLog (@ "Presentationtransitiondidend");

}

-(void) Dismissaltransitiondidend: (BOOL) completed

{

NSLog (@ "Dismissaltransitiondidend");

}

@end

5. To the 4th step, but also only to achieve a custom display, you can listen to the process of display, you can set the display frame.

To implement a custom transition animation, consider setting Second.modaltransitionstyle, but there is no uimodaltransitionstylecustom because

As long as set the Uimodalpresentationcustom, it is equivalent to set the "Uimodaltransitionstylecustom", to achieve custom transitions animation, as long as the corresponding proxy method can be

SECONDVC *second = [[SECONDVC alloc] init];

Second.modalpresentationstyle = Uimodalpresentationcustom;

Second.transitioningdelegate = self;

Second.modaltransitionstyle = uimodaltransitionstylecoververtical; //To implement a custom transition animation, not set here

[Self Presentviewcontroller:second animated:yes completion:nil];

#pragma mark-Proxy method for custom transition animations, two to control display and disappearance, respectively

-(ID <UIViewControllerAnimatedTransitioning>) Animationcontrollerforpresentedcontroller: (Uiviewcontroller * ) presented Presentingcontroller: (Uiviewcontroller *) presenting Sourcecontroller: (Uiviewcontroller *) source

{

return self; //Simple processing, return self, as long as self adheres to <UIViewControllerAnimatedTransitioning> agreement

}

-(ID <UIViewControllerAnimatedTransitioning>) Animationcontrollerfordismissedcontroller: (Uiviewcontroller * ) dismissed

{

return self;

}

6. Implementation of the <UIViewControllerAnimatedTransitioning> protocol method, the following two protocol methods must be implemented

-(Nstimeinterval) Transitionduration: (ID <UIViewControllerContextTransitioning>) transitioncontext{

return 0.3;

}

-(void) Animatetransition: (ID <UIViewControllerContextTransitioning>) transitioncontext{

UIView *toview = [Transitioncontext Viewforkey:uitransitioncontexttoviewkey];

Toview.y =-toview.height;

[UIView animatewithduration:0.3 animations:^{

TOVIEW.Y = 0;

} completion:^ (BOOL finished) {

[Transitioncontext Completetransition:yes]; //This must be written, or the program thinks that the animation is still in the execution, will cause the interface after the display, can not handle the user's Click events

}];

}

This realizes the modal out of the controller, is the transition from the top of the screen animation

7. To realize the animation when the controller disappears, what to do 7.1 add a class that adheres to <uiviewcontrolleranimatedtransitioning> and adds a property to distinguish between showing or disappearing

MYAnimatedTransition.h

@interface myanimatedtransition:nsobject<uiviewcontrolleranimatedtransitioning>

@property (assign,nonatomic) BOOL show;

@end

Myanimatedtransition.m

@implementation Myanimatedtransition

-(Nstimeinterval) Transitionduration: (ID <UIViewControllerContextTransitioning>) transitioncontext

{

return 0.3;

}

-(void) Animatetransition: (ID <UIViewControllerContextTransitioning>) transitioncontext

{

if (self.show = = YES) { //display

UIView *toview = [Transitioncontext Viewforkey:uitransitioncontexttoviewkey];

Toview.y =-toview.height;

[UIView animatewithduration:0.3 animations:^{

TOVIEW.Y = 0;

} completion:^ (BOOL finished) {

[Transitioncontext Completetransition:yes];

}];

}else{ //Disappear

UIView *fromview = [Transitioncontext Viewforkey:uitransitioncontextfromviewkey];

[UIView animatewithduration:0.3 animations:^{

Fromview.y = Fromview.height;

} completion:^ (BOOL finished) {

[Transitioncontext Completetransition:yes];

}];

}

}

@end

7.2 Modifying the Proxy method of the custom transition animation, returning the Myanimatedtransition class object

#pragma mark-Proxy method for custom transition animations, two to control display and disappearance, respectively

-(ID <UIViewControllerAnimatedTransitioning>) Animationcontrollerforpresentedcontroller: (Uiviewcontroller * ) presented Presentingcontroller: (Uiviewcontroller *) presenting Sourcecontroller: (Uiviewcontroller *) source

{

Myanimatedtransition *anima = [[Myanimatedtransition alloc]init];

Anima.show = YES;

Return anima;

}

-(ID <UIViewControllerAnimatedTransitioning>) Animationcontrollerfordismissedcontroller: (Uiviewcontroller * ) dismissed

{

Myanimatedtransition *anima = [[Myanimatedtransition alloc]init];

Anima.show = NO;

Return anima;

}

7.3 Modifying the. m file for custom Uipresentationcontroller

-(void) presentationtransitionwillbegin{

NSLog (@ "Presentationtransitionwillbegin");

[Self.containerview AddSubview:self.presentedView]; //Because it is a custom transition animation, the work of adding a view also needs to be implemented by itself

}

And the following method is obsolete, the program will not be executed, because you are a custom transition animation, the frame before and after the animation has been implemented in this method:-(void) Animatetransition: (ID < uiviewcontrollercontexttransitioning>) Transitioncontext

-(CGRect) Frameofpresentedviewincontainerview

{

Return CGRectMake (100, 100, 150, 300);

}

7.4 To achieve a more complex transition animation, just change the following method, such as changing to a rotation

-(void) Animatetransition: (ID <UIViewControllerContextTransitioning>) transitioncontext

{

UIView *toview = [Transitioncontext Viewforkey:uitransitioncontexttoviewkey];

Toview.y =-toview.height;

ToView.layer.transform = catransform3dmakerotation (m_pi_2, 0, 1, 0);

[UIView animatewithduration:0.3 animations:^{

TOVIEW.Y = 0;

ToView.layer.transform = catransform3didentity;

} completion:^ (BOOL finished) {

[Transitioncontext Completetransition:yes];

}];

}else{

UIView *fromview = [Transitioncontext Viewforkey:uitransitioncontextfromviewkey];

[UIView animatewithduration:0.3 animations:^{

Fromview.y = Fromview.height;

FromView.layer.transform = catransform3dmakerotation (m_pi_2, 0, 1, 0);

} completion:^ (BOOL finished) {

[Transitioncontext Completetransition:yes];

}];

}

}

IOS uses Uipresentationcontroller to customize transition animations

Related Article

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.