How to track long finger hold on iPhone?

How can I track an event, such as touch on the iPhone screen, for 2 seconds. How in Safari to save an image for an image added to UIWebView?

+4
source share
1 answer

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; // do your "held" behavior here } 
+10
source

Source: https://habr.com/ru/post/1300536/


All Articles