首页 > 其他 > 详细

UIPanGestureRecognizer方向判断(摘)

时间:2014-11-30 22:51:31      阅读:906      评论:0      收藏:0      [点我收藏+]

#pragma
mark滑动手势的识别start
typedef enum:NSInteger{
    kCameraMoveDirectionNone,
    kCameraMoveDirectionUp,
    kCameraMoveDirectionDown,
    kCameraMoveDirectionRight,
    kCameraMoveDirectionLeft
}CameraMoveDirection;//枚举方向

 

-(void)panClick:(UIPanGestureRecognizer *) gesture{
    CGPoint translation = [gesture translationInView:self];//最后会有这个方法的说明
    if(gesture.state ==UIGestureRecognizerStateBegan)
    {
        direction = kCameraMoveDirectionNone;
    }
    else if(gesture.state ==UIGestureRecognizerStateChanged&& direction == kCameraMoveDirectionNone)
    {
        direction = [self determineCameraDirectionIfNeeded:translation];//方向函数//方向参数是上面定义的那个枚举类型
        // ok, now initiate movement in the direction indicated by the user‘s gesture
        switch(direction) {
            case kCameraMoveDirectionDown:{
                NSLog(@"Start moving down");break;
            }
            case kCameraMoveDirectionUp:{
                NSLog(@"Start moving up");break;
            }
            case kCameraMoveDirectionRight:
                NSLog(@"Start moving right");
                //和左右滑动的按钮功能搬到这里来break;
            case kCameraMoveDirectionLeft:
                NSLog(@"Start moving left");break;
            default:
                break;
        }
    }
    else if(gesture.state ==UIGestureRecognizerStateEnded)
    {
        // now tell the camera to stop
        NSLog(@"Stop");
    }

}
CGFloat const gestureMinimumTranslation=20.0;//最小形变的决定吧
- (CameraMoveDirection)determineCameraDirectionIfNeeded:(CGPoint)translation
{
    if(direction != kCameraMoveDirectionNone)
        return direction;
    // determine if horizontal swipe only if you meet some minimum velocity
    if(fabs(translation.x) > gestureMinimumTranslation)
    {
        BOOL gestureHorizontal = NO;
        if(translation.y ==0.0)
            gestureHorizontal = YES;
        else
            gestureHorizontal = (fabs(translation.x / translation.y) >5.0);
        if(gestureHorizontal)
        {
            if(translation.x >0.0)
                return kCameraMoveDirectionRight;
            else
                return kCameraMoveDirectionLeft;
        }
    }
    // determine if vertical swipe only if you meet some minimum velocity
    else if(fabs(translation.y) > gestureMinimumTranslation)
    {
        BOOL gestureVertical = NO;
        if(translation.x ==0.0)
            gestureVertical = YES;
        else
            gestureVertical = (fabs(translation.y / translation.x) >5.0);
        if(gestureVertical)
        {
            if(translation.y >0.0)
                return kCameraMoveDirectionDown;
            else
                return kCameraMoveDirectionUp;
        }
    }
    return direction;
}
CGPoint location = [sender locationInView:self.view];
CGPoint translation;
translation.x = location.x - viewStartLocation.x;
translation.y = location.y - viewStartLocation.y;//2个函数的区别,一个坐标,一个是形变量

//这个方法用在了那个日历的插件里面

 

UIPanGestureRecognizer方向判断(摘)

原文:http://www.cnblogs.com/liyang31tg/p/4133747.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!