Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the support for vertical swipe direction. #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions MDCSwipeToChoose/Public/Options/MDCSwipeOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
//

#import <UIKit/UIKit.h>
#import "MDCSwipeDirection.h"

@class MDCPanState;
@class MDCSwipeResult;
Expand Down Expand Up @@ -81,6 +82,12 @@ typedef void (^MDCSwipeToChooseOnChosenBlock)(MDCSwipeResult *state);
*/
@property (nonatomic, assign) CGFloat rotationFactor;

/*!
* Contains the directions on which the swipe will be recognized
* Must be set using a OR operator (like MDCSwipeDirectionUp | MDCSwipeDirectionDown)
*/
@property (nonatomic, assign) MDCSwipeDirection allowedSwipeDirections;

/*!
* A callback to be executed when the view is panned. The block takes an instance of
* `MDCPanState` as an argument. Use this `state` instance to determine the pan direction
Expand Down
1 change: 1 addition & 0 deletions MDCSwipeToChoose/Public/Options/MDCSwipeOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ - (instancetype)init {
_swipeCancelledAnimationOptions = UIViewAnimationOptionCurveEaseOut;
_swipeAnimationDuration = 0.1;
_swipeAnimationOptions = UIViewAnimationOptionCurveEaseIn;
_allowedSwipeDirections = MDCSwipeDirectionLeft | MDCSwipeDirectionRight;
_rotationFactor = 3.f;

_onChosen = [[self class] exitScreenOnChosenWithDuration:0.1
Expand Down
8 changes: 5 additions & 3 deletions MDCSwipeToChoose/Public/State/MDCSwipeDirection.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
* and a swipe to the right as positive.
*/
typedef NS_ENUM(NSInteger, MDCSwipeDirection) {
MDCSwipeDirectionNone = 0,
MDCSwipeDirectionLeft,
MDCSwipeDirectionRight
MDCSwipeDirectionNone = 1,
MDCSwipeDirectionLeft = 2,
MDCSwipeDirectionRight = 4,
MDCSwipeDirectionUp = 8,
MDCSwipeDirectionDown = 16
};
14 changes: 12 additions & 2 deletions MDCSwipeToChoose/Public/Views/UIView+MDCSwipeToChoose.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#import "MDCGeometry.h"
#import <objc/runtime.h>

#define HAS_OPT(options, opt) ((options & opt) == opt)

const void * const MDCSwipeOptionsKey = &MDCSwipeOptionsKey;
const void * const MDCViewStateKey = &MDCViewStateKey;

Expand Down Expand Up @@ -114,6 +116,8 @@ - (void)mdc_setupPanGestureRecognizer {
- (void)mdc_finalizePosition {
MDCSwipeDirection direction = [self mdc_directionOfExceededThreshold];
switch (direction) {
case MDCSwipeDirectionUp:
case MDCSwipeDirectionDown:
case MDCSwipeDirectionRight:
case MDCSwipeDirectionLeft: {
CGPoint translation = MDCCGPointSubtract(self.center,
Expand Down Expand Up @@ -213,10 +217,16 @@ - (CGPoint)mdc_translationExceedingThreshold:(CGFloat)threshold
}

- (MDCSwipeDirection)mdc_directionOfExceededThreshold {
if (self.center.x > self.mdc_viewState.originalCenter.x + self.mdc_options.threshold) {
MDCSwipeDirection directions = [self mdc_options].allowedSwipeDirections;

if (HAS_OPT(directions, MDCSwipeDirectionRight) && self.center.x > self.mdc_viewState.originalCenter.x + self.mdc_options.threshold) {
return MDCSwipeDirectionRight;
} else if (self.center.x < self.mdc_viewState.originalCenter.x - self.mdc_options.threshold) {
} else if (HAS_OPT(directions, MDCSwipeDirectionLeft) && self.center.x < self.mdc_viewState.originalCenter.x - self.mdc_options.threshold) {
return MDCSwipeDirectionLeft;
} else if (HAS_OPT(directions, MDCSwipeDirectionUp) && self.center.y < self.mdc_viewState.originalCenter.y - self.mdc_options.threshold) {
return MDCSwipeDirectionUp;
} else if (HAS_OPT(directions, MDCSwipeDirectionDown) && self.center.y > self.mdc_viewState.originalCenter.y + self.mdc_options.threshold) {
return MDCSwipeDirectionDown;
} else {
return MDCSwipeDirectionNone;
}
Expand Down