【问题标题】:using touchesbegan and touchesmoved to distinguish flick and drag使用 touchesbegan 和 touchesmoved 来区分轻弹和拖动
【发布时间】:2012-12-22 21:37:14
【问题描述】:

我用这两个函数来检测用户在uiview上慢慢拖拽

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

-(void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event

但是,我如何使用这两种方法来检测用户实际轻弹(快速翻转)uiview?

如何区分轻弹和拖动?

非常感谢您的帮助!

正义便士

【问题讨论】:

    标签: iphone ios


    【解决方案1】:

    我认为您应该检查一下手势识别器——它们在区分不同的用户触摸方面需要做很多工作。您所描述的是平移和滑动手势。有特定的手势识别器类来处理其中的每一个。 UIGestureRecognizer 是父类,你应该先看看它。

    【讨论】:

    • 使用手势识别器不是一个好主意,我这样做了,首先交互有点慢,很容易与小uiview的平移和滑动手势混淆。就我而言,uiview 有点小
    【解决方案2】:

    拖动和轻弹通常以速度来区分 - 一种解决方案是创建基于距离公式的算法。

    一个粗略的例子:

    CGPoint pointOld = CGPointMake(0, 0); // Not sure if this is valid
    CGPoint pointNew = CGPointMate(0, 0); // Just making holders for the
                                          // location of the current and previous touches
    
    float timeInterval = 0.2f;
    // However long you think it will take to have enough of a difference in
    // distance to distinguish a flick from a drag
    
    float minFlickDist = 100.0f;
    // Minimum distance traveled in timeInterval to be considered a flick
    
    - (void)callMeEveryTimeInterval
    {
        // Distance formula
        float distBtwnPoints = sqrt( (pointNew.x - pointOld.x) * (pointNew.x - pointOld.x) - (pointNew.y - pointOld.y) * (pointNew.y - pointOld.y) );
        if (distBtwnPoints >= minFlickDist)
        {
            // Flick
        } else {
            // Drag
        }
    }
    

    我认为可能有用的东西的粗略草图 - 希望有所帮助。

    【讨论】:

      【解决方案3】:

      你可以试试这样 使用-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event方法标记开始点和开始时间;使用 -(void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event 方法来标记 endPoint 和 endTime 。然后计算速度,你可以比较速度和你的阈值。(速度可能只计算水平或垂直)

      【讨论】:

      • 虽然不如我的详细,但您的方法实际上在通过使用结束时间而不是安排一个方法来经常检查来节省资源方面做得更好。 +1
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 2011-09-30
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      • 2016-01-28
      相关资源
      最近更新 更多