views:

128

answers:

1

Hi,

How could I track event like touch on iPhone screen for 2 seconds. Like in safari to save image for image added to a UIWebView.. ?

Thank you and Kind Regards, Tharindu Madushanka

+6  A: 

Create an NSTimer with +scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: in your view's -touchesBegan:withEvent: method, and cancel it (using -invalidate) in -touchesEnded:withEvent:. If the method its selector points at gets called, then the user held their finger on the view for whatever duration you set the timer's interval to. Example:

Interface (.h):

@interface MyView : UIView
    ...
    NSTimer *holdTimer;
@end

Implementation (.m):

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)evt
{
    [holdTimer invalidate];
    holdTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(touchWasHeld) userInfo:nil repeats:NO];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)evt
{
    [holdTimer invalidate];
    holdTimer = nil;
}

- (void)touchWasHeld
{
    holdTimer = nil;
    // do your "held" behavior here
}
Noah Witherspoon
That is what I do in a game. Works well.
St3fan