Skip to content

Commit

Permalink
Add Pan animation
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvin Zeng committed Aug 1, 2014
1 parent 3714bf7 commit 4766650
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 2 deletions.
16 changes: 16 additions & 0 deletions AnimationControllers/CEPanAnimationController.h
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
43 changes: 43 additions & 0 deletions AnimationControllers/CEPanAnimationController.m
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
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 InteractionControllers/CEHorizontalSwipeRightInteractionController.m
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
12 changes: 12 additions & 0 deletions TransitionsDemo/TransitionsDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
72EE96D817EF47A60097DF82 /* CECardsAnimationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 72EE96D717EF47A60097DF82 /* CECardsAnimationController.m */; };
72EE96DB17EF66040097DF82 /* CEVerticalSwipeInteractionController.m in Sources */ = {isa = PBXBuildFile; fileRef = 72EE96DA17EF66040097DF82 /* CEVerticalSwipeInteractionController.m */; };
7D0D046C1816DBA700F289A6 /* CENatGeoAnimationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7D0D046B1816DBA700F289A6 /* CENatGeoAnimationController.m */; };
BC466838198B2AC900A07DF8 /* CEHorizontalSwipeRightInteractionController.m in Sources */ = {isa = PBXBuildFile; fileRef = BC466837198B2AC900A07DF8 /* CEHorizontalSwipeRightInteractionController.m */; };
BC46683B198B2E6700A07DF8 /* CEPanAnimationController.m in Sources */ = {isa = PBXBuildFile; fileRef = BC46683A198B2E6700A07DF8 /* CEPanAnimationController.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -96,6 +98,10 @@
72EE96DA17EF66040097DF82 /* CEVerticalSwipeInteractionController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CEVerticalSwipeInteractionController.m; sourceTree = "<group>"; };
7D0D046A1816DBA700F289A6 /* CENatGeoAnimationController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CENatGeoAnimationController.h; sourceTree = "<group>"; };
7D0D046B1816DBA700F289A6 /* CENatGeoAnimationController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CENatGeoAnimationController.m; sourceTree = "<group>"; };
BC466836198B2AC900A07DF8 /* CEHorizontalSwipeRightInteractionController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CEHorizontalSwipeRightInteractionController.h; sourceTree = "<group>"; };
BC466837198B2AC900A07DF8 /* CEHorizontalSwipeRightInteractionController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CEHorizontalSwipeRightInteractionController.m; sourceTree = "<group>"; };
BC466839198B2E6700A07DF8 /* CEPanAnimationController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CEPanAnimationController.h; sourceTree = "<group>"; };
BC46683A198B2E6700A07DF8 /* CEPanAnimationController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CEPanAnimationController.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -185,6 +191,8 @@
7273235717DFBEBF0072C7FD /* AnimationControllers */ = {
isa = PBXGroup;
children = (
BC466839198B2E6700A07DF8 /* CEPanAnimationController.h */,
BC46683A198B2E6700A07DF8 /* CEPanAnimationController.m */,
3CD5090A189FFE6900BAE35A /* CECubeAnimationController.h */,
3CD50909189FFE6900BAE35A /* CECubeAnimationController.m */,
72B92C641856FE98001A2D84 /* CEPortalAnimationController.h */,
Expand Down Expand Up @@ -213,6 +221,8 @@
7273236417DFBF140072C7FD /* InteractionControllers */ = {
isa = PBXGroup;
children = (
BC466836198B2AC900A07DF8 /* CEHorizontalSwipeRightInteractionController.h */,
BC466837198B2AC900A07DF8 /* CEHorizontalSwipeRightInteractionController.m */,
7273236517DFBF140072C7FD /* CEBaseInteractionController.h */,
7273236617DFBF140072C7FD /* CEBaseInteractionController.m */,
7273236717DFBF140072C7FD /* CEHorizontalSwipeInteractionController.h */,
Expand Down Expand Up @@ -330,6 +340,7 @@
72C15E1117E10C620056B3F9 /* CECrossfadeAnimationController.m in Sources */,
72EC05A917E7B1BE00DCB9A3 /* CEPinchInteractionController.m in Sources */,
7273233017DFBD4D0072C7FD /* AppDelegate.m in Sources */,
BC466838198B2AC900A07DF8 /* CEHorizontalSwipeRightInteractionController.m in Sources */,
7273232C17DFBD4D0072C7FD /* main.m in Sources */,
7273236017DFBEBF0072C7FD /* CETurnAnimationController.m in Sources */,
7273236A17DFBF140072C7FD /* CEHorizontalSwipeInteractionController.m in Sources */,
Expand All @@ -341,6 +352,7 @@
7273235F17DFBEBF0072C7FD /* CEFlipAnimationController.m in Sources */,
7273236317DFBEFE0072C7FD /* CEReversibleAnimationController.m in Sources */,
72EE96D817EF47A60097DF82 /* CECardsAnimationController.m in Sources */,
BC46683B198B2E6700A07DF8 /* CEPanAnimationController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
4 changes: 2 additions & 2 deletions TransitionsDemo/TransitionsDemo/SettingsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ @implementation SettingsViewController {

- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
_animationControllers = @[@"None", @"Portal", @"Cards", @"Fold", @"Explode", @"Flip", @"Turn", @"Crossfade", @"NatGeo", @"Cube"];
_interactionControllers = @[@"None", @"HorizontalSwipe", @"VerticalSwipe", @"Pinch"];
_animationControllers = @[@"None", @"Portal", @"Cards", @"Fold", @"Explode", @"Flip", @"Turn", @"Crossfade", @"NatGeo", @"Cube",@"Pan"];
_interactionControllers = @[@"None", @"HorizontalSwipe", @"HorizontalSwipeRight",@"VerticalSwipe", @"Pinch"];
}
return self;
}
Expand Down

0 comments on commit 4766650

Please sign in to comment.