Skip to content

Commit

Permalink
Added interactive gesture
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamazz committed May 22, 2014
1 parent cff1c94 commit 3ba2678
Show file tree
Hide file tree
Showing 12 changed files with 658 additions and 1,489 deletions.
17 changes: 14 additions & 3 deletions AMWaveTransition/AMWaveTransition.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ typedef NS_ENUM(NSInteger, AMWaveTransitionType) {
*/
- (instancetype)initWithOperation:(UINavigationControllerOperation)operation;

/** Attach the interactive gesture
*
* Attach the interactive gesture to the navigation controller. This will pop the current view controller when the user swipes from the left edge.
* Make sure to detach the gesture when done.
*
* @param navigationController The UINavigationController that holds the current view controller
*/
- (void)attachInteractiveGestureToNavigationController:(UINavigationController *)navigationController;

/** Detach the interactive gesture
*
* Detaches the interactive gesture.
*/
- (void)detachInteractiveGesture;

/**-----------------------------------------------------------------------------
* @name AMWaveTransition Properties
Expand Down Expand Up @@ -74,7 +88,4 @@ typedef NS_ENUM(NSInteger, AMWaveTransitionType) {
*/
@property (assign, nonatomic) CGFloat maxDelay;


- (void)attachInteractiveGestureToNavigationController:(UINavigationController *)navigationController;

@end
26 changes: 15 additions & 11 deletions AMWaveTransition/AMWaveTransition.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

#import "AMWaveTransition.h"
//#import <DynamicXray/DynamicXray.h>

@interface AMWaveTransition ()

Expand Down Expand Up @@ -70,8 +69,15 @@ - (void)attachInteractiveGestureToNavigationController:(UINavigationController *
self.animator = [[UIDynamicAnimator alloc]initWithReferenceView:navigationController.view];
self.attachmentsFrom = [@[] mutableCopy];
self.attachmentsTo = [@[] mutableCopy];
// DynamicXray *xray = [[DynamicXray alloc] init];
// [self.animator addBehavior:xray];
}

- (void)detachInteractiveGesture
{
[self.navigationController.view removeGestureRecognizer:self.gesture];
self.navigationController = nil;
self.gesture = nil;
[self.animator removeAllBehaviors];
self.animator = nil;
}

- (void)handlePan:(UIScreenEdgePanGestureRecognizer *)gesture
Expand All @@ -82,7 +88,7 @@ - (void)handlePan:(UIScreenEdgePanGestureRecognizer *)gesture

// Controller that will be visible after the pop
UIViewController<AMWaveTransitioning> *toVC;
int index = [self.navigationController.viewControllers indexOfObject:self.navigationController.topViewController];
int index = (int)[self.navigationController.viewControllers indexOfObject:self.navigationController.topViewController];
toVC = (UIViewController<AMWaveTransitioning> *)self.navigationController.viewControllers[index-1];

// The gesture velocity will also determine the velocity of the cells
Expand All @@ -93,7 +99,7 @@ - (void)handlePan:(UIScreenEdgePanGestureRecognizer *)gesture
[[fromVC visibleCells] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
// The 'selected' cell will be the one leading the other cells
if (CGRectContainsPoint([view.superview convertRect:view.frame toView:nil], touch)) {
self.selectionIndexFrom = idx;
self.selectionIndexFrom = (int)idx;
}
UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc] initWithItem:view
attachedToAnchor:(CGPoint){touch.x, [view.superview convertPoint:view.frame.origin toView:nil].y + view.frame.size.height / 2}];
Expand All @@ -116,7 +122,7 @@ - (void)handlePan:(UIScreenEdgePanGestureRecognizer *)gesture
CGRect futureRect = view.frame;
futureRect.origin.x = 0;
if (CGRectContainsPoint([view.superview convertRect:futureRect toView:nil], touch)) {
self.selectionIndexTo = idx;
self.selectionIndexTo = (int)idx;
}
// TODO: move the setalpha below and scale it
[view setAlpha:1];
Expand All @@ -126,12 +132,11 @@ - (void)handlePan:(UIScreenEdgePanGestureRecognizer *)gesture
[self.animator addBehavior:attachment];
[self.attachmentsTo addObject:attachment];
}];


} else if (gesture.state == UIGestureRecognizerStateChanged) {

[[fromVC visibleCells] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
float delta = touch.x - abs(self.selectionIndexFrom - idx) * velocity / 30;
float delta = touch.x - abs(self.selectionIndexFrom - (int)idx) * velocity / 50;
// Prevent the anchor point from going 'over' the cell
if (delta > view.frame.origin.x + view.frame.size.width / 2) {
delta = view.frame.origin.x + view.frame.size.width / 2 - 2;
Expand All @@ -141,7 +146,7 @@ - (void)handlePan:(UIScreenEdgePanGestureRecognizer *)gesture
}];

[[toVC visibleCells] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
float delta = [gesture locationInView:self.navigationController.view].x - abs(self.selectionIndexTo - idx) * velocity / 30;
float delta = [gesture locationInView:self.navigationController.view].x - abs(self.selectionIndexTo - (int)idx) * velocity / 50;
// Prevent the anchor point from going 'over' the cell
if (delta < view.frame.origin.x + view.frame.size.width / 2) {
delta = view.frame.origin.x + view.frame.size.width / 2 + 2;
Expand Down Expand Up @@ -174,7 +179,6 @@ - (void)handlePan:(UIScreenEdgePanGestureRecognizer *)gesture
view.frame = rect;
}];
} completion:^(BOOL finished) {
[fromVC.view removeFromSuperview];
[self.navigationController popViewControllerAnimated:NO];
}];
} else {
Expand All @@ -191,13 +195,13 @@ - (void)handlePan:(UIScreenEdgePanGestureRecognizer *)gesture
view.frame = rect;
}];
} completion:^(BOOL finished) {
[toVC.view removeFromSuperview];
// Bring 'silently' the cell back to their place, or the normal pop operation would fail
[[toVC visibleCells] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
CGRect rect = view.frame;
rect.origin.x = 0;
view.frame = rect;
}];
[toVC.view removeFromSuperview];
}];

}
Expand Down
9 changes: 9 additions & 0 deletions AMWaveTransition/AMWaveViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

@interface AMWaveViewController () <UINavigationControllerDelegate, AMWaveTransitioning>

@property (strong, nonatomic) IBOutlet AMWaveTransition *interactive;

@end

@implementation AMWaveViewController
Expand All @@ -24,6 +26,13 @@ - (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.navigationController setDelegate:self];
[self.interactive attachInteractiveGestureToNavigationController:self.navigationController];
}

- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[self.interactive detachInteractiveGesture];
}

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
Expand Down
1 change: 0 additions & 1 deletion AMWaveTransition/DynamicXray.framework/DynamicXray

This file was deleted.

1 change: 0 additions & 1 deletion AMWaveTransition/DynamicXray.framework/Headers

This file was deleted.

Binary file not shown.

This file was deleted.

1 change: 0 additions & 1 deletion AMWaveTransition/DynamicXray.framework/Versions/Current

This file was deleted.

Loading

0 comments on commit 3ba2678

Please sign in to comment.