How to iOS 7 Intaraction Transtion - NavigationController
How to iOS 7 Intaraction Transtion (NavigationController)
1.Return animator object in UINavigationControllerDelegate method.
- (id)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
NKRBaseAnimator* animator;
animator = [[NKRBackAnimator alloc] init];
animator.reverse = (operation == UINavigationControllerOperationPop);
animator.duration = 0.3f;
return animator;
}
1.Return UIViewControllerInteractiveTansitioning object in UINavigationControllerDelegate method.
- (id)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id)animationController
{
return self.interactiveTransition;
}
NKRBaseInteraction.h
#import
@interface NKRBaseInteraction : UIPercentDrivenInteractiveTransition
@end
NKRBaseInteraction.m
#import "NKRBaseInteraction.h"
@implementation NKRBaseInteraction
// This is important! if not override this method, animateTransition: was executed twice.
- (CGFloat)completionSpeed
{
return 1 - self.percentComplete;
}
@end
2.Push or Pop view controller in UIGestureRecognizerStateBegan.
if (recognizer.state == UIGestureRecognizerStateBegan) {
// Create a interactive transition
self.interactiveTransition = [[NKRBaseInteraction alloc] init];
if (translation.y > 0) {
// Push view controller
ViewController* viewController;
viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];
viewController.view.backgroundColor = [UIColor colorWithRed:(rand()%255)/255.0f green:(rand()%255)/255.0f blue:(rand()%255/255.0f) alpha:1.0f];
[self.navigationController pushViewController:viewController animated:YES];
}
else {
// Pop view controller
[self.navigationController popViewControllerAnimated:YES];
}
}
3.Do updateInteractiveTransition: in UIGestureRecognizerStateChanged.
else if (recognizer.state == UIGestureRecognizerStateChanged) {
// if an interactive transitions is 100% completed via the user interaction, for some reason
// the animation completion block is not called, and hence the transition is not completed.
// This glorious hack makes sure that this doesn't happen.
// see: https://github.com/ColinEberhardt/VCTransitionsLibrary/issues/4
if (fraction >= 1.0)
fraction = 0.99;
// Update the interactive transition's progress
[self.interactiveTransition updateInteractiveTransition:fraction];
}
4.Do finishInteractiveTransition and cancelInteractiveTransition in UIGestureRecognizerStateEnded and UIGestureRecognizerStateCancelled
else if (recognizer.state == UIGestureRecognizerStateEnded ||
recognizer.state == UIGestureRecognizerStateCancelled) {
// Finish or cancel the interactive transition
if (fraction > 0.5) {
[self.interactiveTransition finishInteractiveTransition];
}
else {
[self.interactiveTransition cancelInteractiveTransition];
}
// Clear interaction
self.interactiveTransition = nil;
}