|
| 1 | +// |
| 2 | +// CEHorizontalSwipeRightInteractionController.m |
| 3 | +// TransitionsDemo |
| 4 | +// |
| 5 | +// Created by Alvin Zeng on 01/08/2014. |
| 6 | +// Copyright (c) 2014 im.luguo. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "CEHorizontalSwipeRightInteractionController.h" |
| 10 | + |
| 11 | +@implementation CEHorizontalSwipeRightInteractionController { |
| 12 | + BOOL _shouldCompleteTransition; |
| 13 | + UIViewController *_viewController; |
| 14 | + UIPanGestureRecognizer *_gesture; |
| 15 | + CEInteractionOperation _operation; |
| 16 | +} |
| 17 | + |
| 18 | +-(void)dealloc { |
| 19 | + [_gesture.view removeGestureRecognizer:_gesture]; |
| 20 | +} |
| 21 | + |
| 22 | +- (void)wireToViewController:(UIViewController *)viewController forOperation:(CEInteractionOperation)operation{ |
| 23 | + _operation = operation; |
| 24 | + _viewController = viewController; |
| 25 | + [self prepareGestureRecognizerInView:viewController.view]; |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +- (void)prepareGestureRecognizerInView:(UIView*)view { |
| 30 | + _gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; |
| 31 | + [view addGestureRecognizer:_gesture]; |
| 32 | +} |
| 33 | + |
| 34 | +- (CGFloat)completionSpeed |
| 35 | +{ |
| 36 | + return 1 - self.percentComplete; |
| 37 | +} |
| 38 | + |
| 39 | +- (void)handleGesture:(UIPanGestureRecognizer*)gestureRecognizer { |
| 40 | + CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view.superview]; |
| 41 | + CGPoint vel = [gestureRecognizer velocityInView:gestureRecognizer.view]; |
| 42 | + |
| 43 | + switch (gestureRecognizer.state) { |
| 44 | + case UIGestureRecognizerStateBegan: { |
| 45 | + |
| 46 | + BOOL leftToRightSwipe = vel.x > 0; |
| 47 | + |
| 48 | + // perform the required navigation operation ... |
| 49 | + |
| 50 | + if (_operation == CEInteractionOperationPop) { |
| 51 | + // for pop operation, fire on right-to-left |
| 52 | + if (leftToRightSwipe) { |
| 53 | + self.interactionInProgress = YES; |
| 54 | + [_viewController.navigationController popViewControllerAnimated:YES]; |
| 55 | + } |
| 56 | + } else if (_operation == CEInteractionOperationTab) { |
| 57 | + // for tab controllers, we need to determine which direction to transition |
| 58 | + if (leftToRightSwipe) { |
| 59 | + if (_viewController.tabBarController.selectedIndex < _viewController.tabBarController.viewControllers.count - 1) { |
| 60 | + self.interactionInProgress = YES; |
| 61 | + _viewController.tabBarController.selectedIndex++; |
| 62 | + } |
| 63 | + |
| 64 | + } else { |
| 65 | + if (_viewController.tabBarController.selectedIndex > 0) { |
| 66 | + self.interactionInProgress = YES; |
| 67 | + _viewController.tabBarController.selectedIndex--; |
| 68 | + } |
| 69 | + } |
| 70 | + } else { |
| 71 | + // for dismiss, fire regardless of the translation direction |
| 72 | + self.interactionInProgress = YES; |
| 73 | + [_viewController dismissViewControllerAnimated:YES completion:nil]; |
| 74 | + } |
| 75 | + break; |
| 76 | + } |
| 77 | + case UIGestureRecognizerStateChanged: { |
| 78 | + if (self.interactionInProgress) { |
| 79 | + // compute the current position |
| 80 | + CGFloat fraction = fabsf(translation.x / 200.0); |
| 81 | + fraction = fminf(fmaxf(fraction, 0.0), 1.0); |
| 82 | + _shouldCompleteTransition = (fraction > 0.5); |
| 83 | + |
| 84 | + //Quick Move |
| 85 | + if(!_shouldCompleteTransition) { |
| 86 | + _shouldCompleteTransition = vel.x > 600; |
| 87 | + } |
| 88 | + |
| 89 | + // if an interactive transitions is 100% completed via the user interaction, for some reason |
| 90 | + // the animation completion block is not called, and hence the transition is not completed. |
| 91 | + // This glorious hack makes sure that this doesn't happen. |
| 92 | + // see: https://github.com/ColinEberhardt/VCTransitionsLibrary/issues/4 |
| 93 | + if (fraction >= 1.0) |
| 94 | + fraction = 0.99; |
| 95 | + |
| 96 | + [self updateInteractiveTransition:fraction]; |
| 97 | + } |
| 98 | + break; |
| 99 | + } |
| 100 | + case UIGestureRecognizerStateEnded: |
| 101 | + case UIGestureRecognizerStateCancelled: |
| 102 | + if (self.interactionInProgress) { |
| 103 | + self.interactionInProgress = NO; |
| 104 | + if (!_shouldCompleteTransition || gestureRecognizer.state == UIGestureRecognizerStateCancelled) { |
| 105 | + [self cancelInteractiveTransition]; |
| 106 | + } |
| 107 | + else { |
| 108 | + [self finishInteractiveTransition]; |
| 109 | + } |
| 110 | + } |
| 111 | + break; |
| 112 | + default: |
| 113 | + break; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | + |
| 118 | +@end |
0 commit comments