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