Create an NSTimer with +scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: in your view -touchesBegan:withEvent: and cancel it (using -invalidate ) in -touchesEnded:withEvent: If the method that indicates its selector is invoked, then the user has swiped the view for any duration specified for the timer interval. 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;
source share