October 2013
Intermediate to advanced
1053 pages
28h 7m
English
You want to be able to detect when the user taps and holds his finger on a view for a certain period of time.
Create an instance of the UILongPressGestureRecognizer class and add it to the view that has to detect
long-tap gestures:
#import "ViewController.h"@interfaceViewController()@property(nonatomic,strong)UILongPressGestureRecognizer*longPressGestureRecognizer;@property(nonatomic,strong)UIButton*dummyButton;@end@implementationViewController
Here is the viewDidLoad instance method of the view
controller that uses the long-press gesture recognizer that we defined in the .m file:
-(void)viewDidLoad{[superviewDidLoad];self.dummyButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];self.dummyButton.frame=CGRectMake(0.0f,0.0f,72.0f,37.0f);[self.dummyButtonsetTitle:@"My button"forState:UIControlStateNormal];self.dummyButton.center=self.view.center;[self.viewaddSubview:self.dummyButton];/* First create the gesture recognizer */self.longPressGestureRecognizer=[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleLongPressGestures:)];/* The number of fingers that must be present on the screen */self.longPressGestureRecognizer.numberOfTouchesRequired=2;/* Maximum 100 points of movement allowed before the gestureis recognized */self.longPressGestureRecognizer.allowableMovement=100.0f;/* The user must press 2 fingers (numberOfTouchesRequired) forat least 1 second ...
Read now
Unlock full access