加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

IOS - UIPanGestureRecognizer 判断方向

(2014-03-06 18:10:34)
标签:

it

分类: Mac/IOS那些事

- (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");

}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有