UIPanGestureRecognizer方向判断(摘)

#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个函数的区别,一个坐标,一个是形变量

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

时间: 2024-10-15 02:44:01

UIPanGestureRecognizer方向判断(摘)的相关文章

UIPanGestureRecognizer上下左右滑动方向判断算法

CGFloat const gestureMinimumTranslation = 20.0; typedef enum :NSInteger { kCameraMoveDirectionNone, kCameraMoveDirectionUp, kCameraMoveDirectionDown, kCameraMoveDirectionRight, kCameraMoveDirectionLeft } CameraMoveDirection; @interfaceViewController

ios开发过程中屏幕方向判断的问题

判断屏幕的方法有很多着及仅提供几个我个人认为好用的方案 Landscape 竖屏 Portrait 横屏 最有效的方法是: 在willRotateToInterfaceOrientation:duration: 方法中将方向存储起来: DrviceOrientation = toInterfaceOrientation; 然后在别的方法中使用相应的屏幕的方向 方法一: 直接获取设备的方法:self.interfaceOrientation(此方法已经过期) 方法二: 通过下面的方法: UIDev

【转载】UIScrollerView滚动方向判断

本文转载自Awei<UIScrollerView滚动方向判断> PushListView.h @interface PushListView : UIView<UITableViewDelegate,UITableViewDataSource { CGFloat contentOffsetY; CGFloat oldContentOffsetY; CGFloat newContentOffsetY; } ............................... PushListVi

input 输入速度和方向判断、搜索功能的延迟请求

1.input 输入速度和方向判断 var wxApp = {} wxApp.click = function (str,speed) { var lastInput = { d: "", flag: true, lastTime: 0, twoClickTime:300, init:function (str,speed) { return { isAdd: this.isAdd(str,speed), speedValid:this.two_click(speed) } }, is

移动端滑动方向判断

移动端滑动方向判断,主要是判断利用x,y轴方向的增量,哪个轴增的快,就是哪个方向. 在touchstart中获取初始点,startX, startY; 在touchmove中获取移动点,moveX, moveY 计算两者的差 deltaX = moveX - startX;  deltaY = moveY - startY; 之后累加deltaX和deltaY: distX += Math.abs(deltaX) distY += Math.abs(deltaY) if (distX > dis

一天一点新东西-鼠标进入容器方向判断

很久之前思考的这个东西,怎么判断鼠标是从哪个方向进入元素的?有代码和一点点解释,用法很精妙,记下来,希望以后看见能记起. index.html <!DOCTYPE html><html><head><style>#wrap{height:100px;width:100px;background-color:#000000;}</style> <script src="http://libs.baidu.com/jquery/1.1

手机端触摸的方向判断

function load(){ document.addEventListener('touchstart',touch, false); document.addEventListener('touchmove',touch, false); document.addEventListener('touchend',touch, false); function touch (event){ var event = event || window.event; var oInp = docu

滚动轴滚动方向判断

开始时的判断代码,通过在滚动函数当中再监听一个滚动函数,然后判断前后的差值来判断是向上滚动还是向下滚动 changeIsDownStatus = () => { const listView = this.listView; const beforeScrollTop = listView.scrollTop; this.listView.addEventListener('scroll', () => { const afterScrollTop = listView.scrollTop;

移动应用滑动屏幕方向判断解决方案,JS判断手势方向

问题分类 滑动屏幕打开相应功能操作. 问题描述 1.用户手动滑动屏幕,根据滑动的方向,打开相应的功能(如:向上滑摇钱树经验明细,向下滑打开任务明细,向左滑打开聚宝盆物品查看等功能),滑动事件捕获问题. 2.大家都知道,划动都有角度问题,如:向330度方向滑动手机,要计算出它的方向问题. 3.HTML5提供的滑动事件,只能读取到起点和终点坐标,计算角度问题. 4.手机屏幕坐标与标准坐标系转换问题. 解决方案 1.滑动屏幕事件使用HTML5 的touchstart滑动开始事件和touchend滑动结