Skip to content

Commit 78cf23f

Browse files
Merge branch 'master' of https://github.com/zxy198717/VCTransitionsLibrary into zxy198717-master
2 parents 6643a6d + 1d0d0f7 commit 78cf23f

File tree

12 files changed

+212
-4
lines changed

12 files changed

+212
-4
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// CEPanAnimationController.h
3+
// TransitionsDemo
4+
//
5+
// Created by Alvin Zeng on 01/08/2014.
6+
// Copyright (c) 2014 Alvin Zeng. All rights reserved.
7+
//
8+
9+
#import "CEReversibleAnimationController.h"
10+
11+
/**
12+
Animates between the two view controllers by performing a simple pan.
13+
*/
14+
@interface CEPanAnimationController : CEReversibleAnimationController
15+
16+
@end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// CEPanAnimationController.m
3+
// TransitionsDemo
4+
//
5+
// Created by Alvin Zeng on 01/08/2014.
6+
// Copyright (c) 2014 Alvin Zeng. All rights reserved.
7+
//
8+
9+
#import "CEPanAnimationController.h"
10+
11+
@implementation CEPanAnimationController
12+
13+
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext fromVC:(UIViewController *)fromVC toVC:(UIViewController *)toVC fromView:(UIView *)fromView toView:(UIView *)toView {
14+
15+
self.duration = .3;
16+
// Add the toView to the container
17+
UIView* containerView = [transitionContext containerView];
18+
[containerView addSubview:toView];
19+
toView.frame = CGRectMake(self.reverse ? -160 : 320, toView.frame.origin.y, toView.frame.size.width, toView.frame.size.height);
20+
21+
self.reverse ? [containerView sendSubviewToBack:toView] : [containerView bringSubviewToFront:toView];
22+
23+
// animate
24+
NSTimeInterval duration = [self transitionDuration:transitionContext];
25+
[UIView animateWithDuration:duration animations:^{
26+
fromView.frame = CGRectMake(!self.reverse ? -160 : 320, fromView.frame.origin.y, fromView.frame.size.width, fromView.frame.size.height);
27+
toView.frame = CGRectMake(0, toView.frame.origin.y, toView.frame.size.width, toView.frame.size.height);
28+
} completion:^(BOOL finished) {
29+
if ([transitionContext transitionWasCancelled]) {
30+
toView.frame = CGRectMake(0, toView.frame.origin.y, toView.frame.size.width, toView.frame.size.height);
31+
fromView.frame = CGRectMake(0, fromView.frame.origin.y, fromView.frame.size.width, fromView.frame.size.height);
32+
} else {
33+
// reset from- view to its original state
34+
[fromView removeFromSuperview];
35+
fromView.frame = CGRectMake(!self.reverse ? -160 : 320, fromView.frame.origin.y, fromView.frame.size.width, fromView.frame.size.height);
36+
toView.frame = CGRectMake(0, toView.frame.origin.y, toView.frame.size.width, toView.frame.size.height);
37+
}
38+
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
39+
}];
40+
41+
}
42+
43+
@end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// CEHorizontalSwipeRightInteractionController.h
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 <Foundation/Foundation.h>
10+
#import "CEBaseInteractionController.h"
11+
12+
/**
13+
A horizontal swipe interaction controller. When used with a navigation controller, a left-to-right swipe
14+
will cause a 'pop' navigation. When used wth a tabbar controller, right-to-left and left-to-right cause navigation
15+
between neighbouring tabs.
16+
*/
17+
@interface CEHorizontalSwipeRightInteractionController : CEBaseInteractionController
18+
19+
@end
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ The library currently contains the following animations, which can be made inter
3131
</tr>
3232
<tr>
3333
<th>Cube</th>
34-
<th></th>
34+
<th>Pan</th>
3535
<th></th>
3636
<th></th>
3737
</tr>
3838
<tr>
3939
<td><img src="Screenshots/thumbnails/Cube/2.png"/></td>
40-
<td></td>
40+
<td><img src="Screenshots/thumbnails/Pan/pan_2.png"/></td>
4141
<td></td>
4242
<td></td>
4343
</tr>

Screenshots/Pan/pan_1.png

258 KB
Loading

Screenshots/Pan/pan_2.png

200 KB
Loading

Screenshots/Pan/pan_3.png

301 KB
Loading
25.4 KB
Loading
18.4 KB
Loading

0 commit comments

Comments
 (0)