IOS custom modal controller Animation

Source: Internet
Author: User

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
  
 

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.