IOS transition Animation

Source: Internet
Author: User

IOS transition Animation

Generally, we use two types of transfer push and present.

Present

/** 1. set proxy-(instancetype) init {self = [super init]; if (self) {self. transitioningDelegate = self; self. modalPresentationStyle = UIModalPresentationCustom;} return self;} 2. add protocol <UIViewControllerTransitioningDelegate> 3. rewrite protocol method-Initialize in Protocol method-(id <UIViewControllerAnimatedTransitioning>) animationControllerForPresentedController :( UIViewController *) presented presentingController :( UIViewController *) presenting sourceController :( UIViewController *) source {}-(id <UIViewControllerAnimatedTransitioning>) animationControllerForDismissedController :( UIViewController *) dismissed {}**/

2. push

When the first controller pushes the second Controller

ViewController2 * vc = [[ViewController2 alloc] init]; self. navigationController. delegate = vc; // you must enter this sentence [self. navigationController pushViewController: vc animated: YES];

Second Controller

@interface ViewController2 : UIViewController<UINavigationControllerDelegate>@end

Proxy Method

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {}

The above is the place where push and present occur, both need to return a UIViewControllerAnimatedTransitioning

So we write a class

@ Interface WJTransiation_Mobile: NSObject <UIViewControllerAnimatedTransitioning>

@ End

Then modify the Protocol Method

// Animation time

-(NSTimeInterval) transitionDuration :( nullable id <UIViewControllerContextTransitioning>) transitionContext;

// Custom transition Animation

-(Void) animateTransition :( id <UIViewControllerContextTransitioning>) transitionContext;

Execute transition Animation

1. Get the front and back Controllers

   UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

2. Obtain the transition animation View

UIView *containerView = [transitionContext containerView];

3. Add the view to which the front and back controllers need to be animated to containerView.

   [containerView addSubview:tempView];    [containerView addSubview:toVC.view];

4. Execute your own animation

Demo link: http://pan.baidu.com/s/1kTPPUMb

:

Supplement:

// Dynamically Add the transiationView attribute @ interface UIViewController (transiationView) @ property (nonatomic, strong) UIView * transiationView; @ end
#import <objc/runtime.h>@implementation UIViewController (transiationView)static char transiationViewKey;- (void)setTransiationView:(UIView *)transiationView {    return objc_setAssociatedObject(self, &transiationViewKey, transiationView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (UIView *)transiationView {     return objc_getAssociatedObject(self, &transiationViewKey);}@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.