2010-03-09 90 views
1

我有一个非常简单的UIButton的子类,当按钮被保持2秒时,它将触发一个自定义事件。要做到这一点,我推翻:continueTrackingWithTouch:withEvent:不被连续调用

// 
// 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); 
} 

我的问题是(这是在模拟器上,无法在设备上的工作做的相当尚)“continueTrackingWithTouch:withEvent:方法”不会被调用,除非鼠标实际移动。它并不需要太多动作,但确实需要一些动作。

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

注意:userInteractionEnabled设置为YES。注2:我可以在beginTrackingWithTouch:withEvent中设置一个计时器:但是对于那些应该很简单的东西来说,这似乎更加努力。

回答

2

您描述的行为是预期行为。跟踪只是为了跟踪移动。

为了测试所述触摸的持续时间,我建议开始在beginTrackingWithTouch一个计时器,2秒后触发事件,并且在endTrackingWithTouch一个测试情况下取消定时器。