IOS custom modal controller Animation
The presentViewController animation comes from the drill-down, but sometimes we need to customize the modal animation.
// Original method-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {RedViewController * redVc = [[RedViewController alloc] init]; redVc. view. backgroundColor = [UIColor redColor]; [self presentViewController: redVc animated: YES completion: nil];}
Current Method
-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {RedViewController * redVc = [[RedViewController alloc] init]; // customize the modal mode redVc. modalPresentationStyle = UIModalPresentationCustom; // The delegate redVc of the transition. transitioningDelegate = [ZLTransitioning sharedTransitioning]; redVc. view. backgroundColor = [UIColor redColor]; [self presentViewController: redVc animated: YES completion: nil];}
ZLTransitioning. h implements the UIViewControllerTransitioningDelegate proxy method.
@ Interface ZLTransitioning: NSObject
// Create a singleton instance
+ (Instancetype) sharedTransitioning;
@ End
ZLTransitioning. m file
ZLAnimatedTransitioning encapsulates the methods in the UIViewControllerAnimatedTransitioning proxy protocol.
# Import "ZLTransitioning. h" # import "placement. h" # import "PresentationVc. h" @ implementation ZLTransitioningstatic id _ instance = nil; + (instancetype) sharedTransitioning {if (! _ Instance) {static dispatch_once_t onceToken; dispatch_once (& onceToken, ^ {_ instance = [[self alloc] init] ;});} return _ instance ;} # pragma mark-PrsentView-(UIPresentationController *) Comment :( UIViewController *) presented presentingViewController :( UIViewController *) presenting sourceViewController :( UIViewController *) source {return [PresentationVc alloc] Comment: presented presentingViewController: presenting] ;}# pragma mark-start animation call-(id
) Parameters :( UIViewController *) presented presentingController :( UIViewController *) presenting sourceController :( UIViewController *) source {ZLAnimatedTransitioning * startA = [[ZLAnimatedTransitioning alloc] init]; startA. presented = YES; return startA;} # pragma mark-disMiss call-(id
) AnimationControllerForDismissedController :( UIViewController *) dismissed {ZLAnimatedTransitioning * endA = [[ZLAnimatedTransitioning alloc] init]; endA. presented = NO; return endA;} @ end
#import
@interface PresentationVc : UIPresentationController@end
# Import "PresentationVc. h "@ implementation PresentationVc # pragma mark-frame of the set size, useless for Custom Animation //-(CGRect) frameOfPresentedViewInContainerView {// return CGRectMake (10, 20,200,200 ); //} # pragma mark-start animation added-(void) presentationTransitionWillBegin {[self. containerView addSubview: self. presentedView];}-(void) presentationTransitionDidEnd :( BOOL) completed {NSLog (@ "region");}-(void) dismissalTransitionWillBegin {NSLog (@ "region ");} # removed after The pragma mark-dismiss animation ends-(void) dismissalTransitionDidEnd :( BOOL) completed {[self. presentedView removeFromSuperview]; // NSLog (@ "dismissalTransitionDidEnd");} @ end
ZLAnimatedTransitioning is a method encapsulated in the UIViewControllerAnimatedTransitioning proxy protocol.
# Import
@ Interface ZLAnimatedTransitioning: NSObject
// Whether the @ property (nonatomic, assign) BOOL presented; @ end
ZLAnimatedTransitioning. m
# Import "audio. h" @ implementation ZLAnimatedTransitioning // animation execution time static const CGFloat duration = 0.5; # pragma mark-return animation execution time-(NSTimeInterval) transitionDuration :( id
) TransitionContext {return duration;} # pragma mark-specific instance for executing the animation-(void) animateTransition :( id
) TransitionContext {if (self. presented) {UIView * toView = [transitionContext viewForKey: UITransitionContextToViewKey]; _ block CGRect tempFrame = toView. frame; tempFrame. origin. y =-toView. frame. size. height; toView. frame = tempFrame; [UIView animateWithDuration: duration animations: ^ {tempFrame. origin. y = 0; toView. frame = tempFrame;} completion: ^ (BOOL finished) {[transitionContext completeTransition: YES] ;}];} else {UIView * toView = [transitionContext viewForKey: UITransitionContextFromViewKey]; _ block CGRect tempFrame = toView. frame; [UIView animateWithDuration: duration animations: ^ {tempFrame. origin. y =-toView. frame. size. height; toView. frame = tempFrame;} completion: ^ (BOOL finished) {[transitionContext completeTransition: YES] ;}}@ end