代码之家  ›  专栏  ›  技术社区  ›  Steven Noyes

继续使用touch:withevent:不连续调用

  •  1
  • Steven Noyes  · 技术社区  · 15 年前

    我有一个非常简单的uibutton子类,当按钮被保持2秒时,它将触发一个自定义事件。为了完成这一点,我骑过了:

    //
    // Mouse tracking
    //
    - (BOOL)beginTrackingWithTouch:(UITouch *)touch 
                         withEvent:(UIEvent *)event
    {
        [super beginTrackingWithTouch:touch withEvent:event];
        [self setTimeButtonPressed:[NSDate date]];
    
        return (YES);
    }
    
    - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
    {
        [super continueTrackingWithTouch:touch withEvent:event];
        if ([[self timeButtonPressed] timeIntervalSinceNow] > 2.0)
        {
            //
            // disable the button.  This will eventually fire the event
            // but this is just stubbed to act as a break point area.
            //
            [self setEnabled:NO];
            [self setSelected:NO];
        }
    
        return (YES);
    }
    

    我的问题是(这是在模拟器中,还不能在设备上工作)“用触摸继续跟踪:用事件:”除非鼠标实际移动,否则不会被调用。它不需要太多的运动,但需要一些运动。

    通过在这两个选项中返回“是”,我应该设置为接收连续事件。这是仿真器的怪事还是我做错了什么?

    注意:UserInteractionEnabled设置为Yes。

    注2:我可以在BeginTrackingWithTouch:WithEvent:中设置一个计时器,但对于一些应该很简单的事情,这似乎需要更多的努力。

    1 回复  |  直到 15 年前
        1
  •  2
  •   kbanman    15 年前

    你所描述的行为就是预期的行为。跟踪只是为了跟踪运动。

    为了测试触摸持续时间,我建议在 beginTrackingWithTouch 在2秒后触发事件,并在 endTrackingWithTouch 取消计时器。