Created
October 15, 2014 11:14
-
-
Save IgorMuzyka/532f3d2bd6a6abb34494 to your computer and use it in GitHub Desktop.
Directional gesture recognizer
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
// | |
// MMDirectionPanGestureRecognizer.h | |
// MMSimplifiedTransitions | |
// | |
// Created by Igor Muzyka on 8/3/14. | |
// Copyright (c) 2014 Igor Muzyka. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
typedef enum { | |
MMDirectionPanGestureRecognizerDirectionNone, | |
MMDirectionPanGestureRecognizerDirectionUp, | |
MMDirectionPanGestureRecognizerDirectionDown, | |
MMDirectionPanGestureRecognizerDirectionLeft, | |
MMDirectionPanGestureRecognizerDirectionRight | |
} MMDirectionPanGestureRecognizerDirection; | |
@interface MMDirectionPanGestureRecognizer : UIPanGestureRecognizer | |
+ (instancetype)setupInViewController:(UIViewController *)viewController; | |
@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
// | |
// MMDirectionPanGestureRecognizer.m | |
// MMSimplifiedTransitions | |
// | |
// Created by Igor Muzyka on 8/3/14. | |
// Copyright (c) 2014 Igor Muzyka. All rights reserved. | |
// | |
#import "MMDirectionPanGestureRecognizer.h" | |
@interface MMDirectionPanGestureRecognizer () | |
@property (nonatomic, assign) MMDirectionPanGestureRecognizerDirection direction; | |
@property (nonatomic, strong) UIViewController *containingViewController; | |
@end | |
@implementation MMDirectionPanGestureRecognizer | |
- (id)initWithTarget:(UIViewController *)target action:(SEL)action { | |
self = [super initWithTarget:self action:action]; | |
if (self) { | |
self.containingViewController = target; | |
} | |
return self; | |
} | |
+ (instancetype)setupInViewController:(UIViewController *)viewController { | |
MMDirectionPanGestureRecognizer *recognizer = [[self alloc] initWithTarget:viewController action:@selector(handleDirectionalPanGestureRecognizer:)]; | |
[viewController.view addGestureRecognizer:recognizer]; | |
return recognizer; | |
} | |
- (void)handleDirectionalPanGestureRecognizer:(MMDirectionPanGestureRecognizer *)recognizer { | |
switch (recognizer.state) { | |
case UIGestureRecognizerStateBegan: | |
[self didBeganGesture]; | |
break; | |
case UIGestureRecognizerStateEnded: | |
[self didEndGesture]; | |
break; | |
case UIGestureRecognizerStateCancelled: | |
[self didCancelledGesture]; | |
break; | |
case UIGestureRecognizerStateChanged: | |
[self stateChanged]; | |
break; | |
default: | |
break; | |
} | |
} | |
#pragma mark - Life Cycle | |
- (void)didBeganGesture { | |
[self updateDirectionForCurrentState]; | |
} | |
- (void)didEndGesture { | |
[self updateDirectionForCurrentState]; | |
} | |
- (void)stateChanged { | |
} | |
- (void)didCancelledGesture { | |
} | |
#pragma mark - Helper methods | |
- (void)updateDirectionForCurrentState { | |
if (self.state == UIGestureRecognizerStateBegan) { | |
CGPoint velocity = [self velocityInView:self.containingViewController.view]; | |
if (abs(velocity.x) > abs(velocity.y)) { | |
if (velocity.x > 0) { | |
self.direction = MMDirectionPanGestureRecognizerDirectionRight; | |
} else { | |
self.direction = MMDirectionPanGestureRecognizerDirectionLeft; | |
} | |
} else { | |
if (velocity.y > 0) { | |
self.direction = MMDirectionPanGestureRecognizerDirectionDown; | |
} else { | |
self.direction = MMDirectionPanGestureRecognizerDirectionUp; | |
} | |
} | |
} else if (self.state == UIGestureRecognizerStateCancelled || self.state == UIGestureRecognizerStateEnded || self.state == UIGestureRecognizerStateFailed){ | |
self.direction = MMDirectionPanGestureRecognizerDirectionNone; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment