- (void)pan:(UIPanGestureRecognizer *)sender
{
typedef NS_ENUM(NSUInteger,
UIPanGestureRecognizerDirection) {
UIPanGestureRecognizerDirectionUndefined,
UIPanGestureRecognizerDirectionUp,
UIPanGestureRecognizerDirectionDown,
UIPanGestureRecognizerDirectionLeft,
UIPanGestureRecognizerDirectionRight
};
static UIPanGestureRecognizerDirection
direction =
UIPanGestureRecognizerDirectionUndefined;
switch (sender.state) {
case
UIGestureRecognizerStateBegan:
{
if
(direction ==
UIPanGestureRecognizerDirectionUndefined) {
CGPoint velocity = [sender velocityInView:self.view];
BOOL isVerticalGesture = fabs(velocity.y) > fabs(velocity.x);
if (isVerticalGesture) {
if
(velocity.y > 0) {
direction
= UIPanGestureRecognizerDirectionDown;
} else {
direction
= UIPanGestureRecognizerDirectionUp;
}
}
else {
if
(velocity.x > 0) {
direction
= UIPanGestureRecognizerDirectionRight;
} else {
direction
= UIPanGestureRecognizerDirectionLeft;
}
}
}
break;
}
case
UIGestureRecognizerStateChanged:
{
switch (direction) {
case
UIPanGestureRecognizerDirectionUp:
{
[self
handleUpwardsGesture:sender];
break;
}
case
UIPanGestureRecognizerDirectionDown:
{
[self
handleDownwardsGesture:sender];
break;
}
case
UIPanGestureRecognizerDirectionLeft:
{
[self
handleLeftGesture:sender];
break;
}
case
UIPanGestureRecognizerDirectionRight:
{
[self
handleRightGesture:sender];
break;
}
default: {
break;
}
}
}
case
UIGestureRecognizerStateEnded:
{
direction =
UIPanGestureRecognizerDirectionUndefined;
break;
}
default:
break;
}
}
-
(void)handleUpwardsGesture:(UIPanGestureRecognizer *)sender
{
NSLog(@"Up");
}
-
(void)handleDownwardsGesture:(UIPanGestureRecognizer *)sender
{
NSLog(@"Down");
}
-
(void)handleLeftGesture:(UIPanGestureRecognizer *)sender
{
NSLog(@"Left");
}
-
(void)handleRightGesture:(UIPanGestureRecognizer *)sender
{
NSLog(@"Right");
}
加载中,请稍候......