forked from ColinEberhardt/VCTransitionsLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alvin Zeng
committed
Aug 1, 2014
1 parent
3714bf7
commit 4766650
Showing
6 changed files
with
210 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// CEPanAnimationController.h | ||
// TransitionsDemo | ||
// | ||
// Created by Alvin Zeng on 01/08/2014. | ||
// Copyright (c) 2014 Alvin Zeng. All rights reserved. | ||
// | ||
|
||
#import "CEReversibleAnimationController.h" | ||
|
||
/** | ||
Animates between the two view controllers by performing a simple pan. | ||
*/ | ||
@interface CEPanAnimationController : CEReversibleAnimationController | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// CEPanAnimationController.m | ||
// TransitionsDemo | ||
// | ||
// Created by Alvin Zeng on 01/08/2014. | ||
// Copyright (c) 2014 Alvin Zeng. All rights reserved. | ||
// | ||
|
||
#import "CEPanAnimationController.h" | ||
|
||
@implementation CEPanAnimationController | ||
|
||
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext fromVC:(UIViewController *)fromVC toVC:(UIViewController *)toVC fromView:(UIView *)fromView toView:(UIView *)toView { | ||
|
||
self.duration = .3; | ||
// Add the toView to the container | ||
UIView* containerView = [transitionContext containerView]; | ||
[containerView addSubview:toView]; | ||
toView.frame = CGRectMake(self.reverse ? -160 : 320, toView.frame.origin.y, toView.frame.size.width, toView.frame.size.height); | ||
|
||
self.reverse ? [containerView sendSubviewToBack:toView] : [containerView bringSubviewToFront:toView]; | ||
|
||
// animate | ||
NSTimeInterval duration = [self transitionDuration:transitionContext]; | ||
[UIView animateWithDuration:duration animations:^{ | ||
fromView.frame = CGRectMake(!self.reverse ? -160 : 320, fromView.frame.origin.y, fromView.frame.size.width, fromView.frame.size.height); | ||
toView.frame = CGRectMake(0, toView.frame.origin.y, toView.frame.size.width, toView.frame.size.height); | ||
} completion:^(BOOL finished) { | ||
if ([transitionContext transitionWasCancelled]) { | ||
toView.frame = CGRectMake(0, toView.frame.origin.y, toView.frame.size.width, toView.frame.size.height); | ||
fromView.frame = CGRectMake(0, fromView.frame.origin.y, fromView.frame.size.width, fromView.frame.size.height); | ||
} else { | ||
// reset from- view to its original state | ||
[fromView removeFromSuperview]; | ||
fromView.frame = CGRectMake(!self.reverse ? -160 : 320, fromView.frame.origin.y, fromView.frame.size.width, fromView.frame.size.height); | ||
toView.frame = CGRectMake(0, toView.frame.origin.y, toView.frame.size.width, toView.frame.size.height); | ||
} | ||
[transitionContext completeTransition:![transitionContext transitionWasCancelled]]; | ||
}]; | ||
|
||
} | ||
|
||
@end |
19 changes: 19 additions & 0 deletions
19
InteractionControllers/CEHorizontalSwipeRightInteractionController.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// CEHorizontalSwipeRightInteractionController.h | ||
// TransitionsDemo | ||
// | ||
// Created by Alvin Zeng on 01/08/2014. | ||
// Copyright (c) 2014 im.luguo. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "CEBaseInteractionController.h" | ||
|
||
/** | ||
A horizontal swipe interaction controller. When used with a navigation controller, a left-to-right swipe | ||
will cause a 'pop' navigation. When used wth a tabbar controller, right-to-left and left-to-right cause navigation | ||
between neighbouring tabs. | ||
*/ | ||
@interface CEHorizontalSwipeRightInteractionController : CEBaseInteractionController | ||
|
||
@end |
118 changes: 118 additions & 0 deletions
118
InteractionControllers/CEHorizontalSwipeRightInteractionController.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// | ||
// CEHorizontalSwipeRightInteractionController.m | ||
// TransitionsDemo | ||
// | ||
// Created by Alvin Zeng on 01/08/2014. | ||
// Copyright (c) 2014 im.luguo. All rights reserved. | ||
// | ||
|
||
#import "CEHorizontalSwipeRightInteractionController.h" | ||
|
||
@implementation CEHorizontalSwipeRightInteractionController { | ||
BOOL _shouldCompleteTransition; | ||
UIViewController *_viewController; | ||
UIPanGestureRecognizer *_gesture; | ||
CEInteractionOperation _operation; | ||
} | ||
|
||
-(void)dealloc { | ||
[_gesture.view removeGestureRecognizer:_gesture]; | ||
} | ||
|
||
- (void)wireToViewController:(UIViewController *)viewController forOperation:(CEInteractionOperation)operation{ | ||
_operation = operation; | ||
_viewController = viewController; | ||
[self prepareGestureRecognizerInView:viewController.view]; | ||
} | ||
|
||
|
||
- (void)prepareGestureRecognizerInView:(UIView*)view { | ||
_gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; | ||
[view addGestureRecognizer:_gesture]; | ||
} | ||
|
||
- (CGFloat)completionSpeed | ||
{ | ||
return 1 - self.percentComplete; | ||
} | ||
|
||
- (void)handleGesture:(UIPanGestureRecognizer*)gestureRecognizer { | ||
CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view.superview]; | ||
CGPoint vel = [gestureRecognizer velocityInView:gestureRecognizer.view]; | ||
|
||
switch (gestureRecognizer.state) { | ||
case UIGestureRecognizerStateBegan: { | ||
|
||
BOOL leftToRightSwipe = vel.x > 0; | ||
|
||
// perform the required navigation operation ... | ||
|
||
if (_operation == CEInteractionOperationPop) { | ||
// for pop operation, fire on right-to-left | ||
if (leftToRightSwipe) { | ||
self.interactionInProgress = YES; | ||
[_viewController.navigationController popViewControllerAnimated:YES]; | ||
} | ||
} else if (_operation == CEInteractionOperationTab) { | ||
// for tab controllers, we need to determine which direction to transition | ||
if (leftToRightSwipe) { | ||
if (_viewController.tabBarController.selectedIndex < _viewController.tabBarController.viewControllers.count - 1) { | ||
self.interactionInProgress = YES; | ||
_viewController.tabBarController.selectedIndex++; | ||
} | ||
|
||
} else { | ||
if (_viewController.tabBarController.selectedIndex > 0) { | ||
self.interactionInProgress = YES; | ||
_viewController.tabBarController.selectedIndex--; | ||
} | ||
} | ||
} else { | ||
// for dismiss, fire regardless of the translation direction | ||
self.interactionInProgress = YES; | ||
[_viewController dismissViewControllerAnimated:YES completion:nil]; | ||
} | ||
break; | ||
} | ||
case UIGestureRecognizerStateChanged: { | ||
if (self.interactionInProgress) { | ||
// compute the current position | ||
CGFloat fraction = fabsf(translation.x / 200.0); | ||
fraction = fminf(fmaxf(fraction, 0.0), 1.0); | ||
_shouldCompleteTransition = (fraction > 0.5); | ||
|
||
//Quick Move | ||
if(!_shouldCompleteTransition) { | ||
_shouldCompleteTransition = vel.x > 600; | ||
} | ||
|
||
// 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; | ||
|
||
[self updateInteractiveTransition:fraction]; | ||
} | ||
break; | ||
} | ||
case UIGestureRecognizerStateEnded: | ||
case UIGestureRecognizerStateCancelled: | ||
if (self.interactionInProgress) { | ||
self.interactionInProgress = NO; | ||
if (!_shouldCompleteTransition || gestureRecognizer.state == UIGestureRecognizerStateCancelled) { | ||
[self cancelInteractiveTransition]; | ||
} | ||
else { | ||
[self finishInteractiveTransition]; | ||
} | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters