views:

176

answers:

1

I have a very simply subclass of UIButton that will fire off a custom event when the button has been held for 2 seconds. To accomplish this, I overrode:

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

My problem is (this is in the simulator, can't do on device work quite yet) "continueTrackingWithTouch: withEvent:" is not being called unless the mouse is actually moved. It does not take much motion, but it does take some motion.

By returning "YES" in both of these, I should be setup to receive continuous events. Is this an oddity of the simulator or am I doing something wrong?

NOTE: userInteractionEnabled is set to YES.

NOTE2: I could setup a timer in beginTrackingWithTouch: withEvent: but that seems like more effort for something that should be simple.

+1  A: 

The behaviour you're describing is the intended behaviour. Tracking is only to keep track of movement.

To test for duration of the touch, I would suggest to start a timer in beginTrackingWithTouch that fires the event after 2 seconds, and a test case in endTrackingWithTouch that cancels the timer.

Kelso.b