Gesture refers to all events that occur when you touch the screen with one or more fingers until all your fingers exit the screen. The UIGestureRecognizer is an object that knows how to observe user-generated event streams and recognize when users pull, touch, and drag in a way that matches a predefined gesture. The UIGestureRecognizer class encapsulates the search gesture work. In the simulator, press the "option" key to simulate the gesture of two fingers.
Create a view object in the. h file. All gestures are performed on the View:
@ Interface LinViewController: UIViewController // create a view object @ property (retain, nonatomic) UIView * mView; @ end
In the. m file, create the object of each gesture and compile the implementation method of each gesture:
@ Implementation LinViewController-(void) viewDidLoad {[super viewDidLoad]; // --- click the gesture reader --- // create the object ** pSingleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (singleGesture :)]; // Add the gesture to the current view [self. view addGestureRecognizer: pSingleTap]; // create the click gesture object UITapGestureRecognizer * pDoubleTap = [[delealloc] initWithTarget: self action: @ selector (doubleGesture :)]; // set the number of clicks to 2. The default value is 1, which is a click operation pDoubleTap. numberOfTapsRequired = 2; // Add the gesture to the current view [self. view addGestureRecognizer: pDoubleTap]; // when you double-click [pSingleTap restart: pDoubleTap]; // release the created object [pDoubleTap release]; [pSingleTap release]; // --- slide gesture reader --- // create the slide gesture object watermark * pSwip = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @ selector (swipGesture :)]; // set the slide direction. Here, it is right. There are 4 pSwip directions. direction = UISwipeGestureRecognizerDirectionRight; // Add the gesture to the current view [self. view addGestureRecognizer: pSwip]; // release the created object [pSwip release]; // --- drag the gesture identification tool -- // create a drag gesture object UIPanGestureRecognizer * pPan = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (panGesture :)]; // Add the gesture to the current view [self. view addGestureRecognizer: pPan]; // release the created object [pPan release]; // --- long-pressed gesture reader -- // create the object of Long-pressed gesture. Optional * pLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @ selector (longPressGesture :)]; // set the long press time, which is determined to be a long press of pLongPress for more than 2 seconds. minimumPressDuration = 2; // Add the gesture to the current view [self. view addGestureRecognizer: pLongPress]; // release the created object [pLongPress release]; // --- rotate gesture reader --- // create a rotate gesture object UIRotationGestureRecognizer * pRotation = [[UIRotationGestureRecognizer alloc] initWithTarget: self action: @ selector (rotationGesture :)]; // Add the gesture to the current view [self. view addGestureRecognizer: pRotation]; // release the created object [pRotation release];} # pragma mark -------- tapGesture >>> single click-(void) singleGesture :( optional *) tap {// click NSLog (@ "Click operation");} # pragma mark -------- tapGesture >>> double-(void) doubleGesture :( UITapGestureRecognizer *) tap {// double-click NSLog (@ "double-click operation");} # pragma mark -------- tapGesture >>> Swip slide-(void) swipGesture :( UISwipeGestureRecognizer *) swip {// operation NSLog corresponding to the screen gesture (@ "Sliding operation, split direction, slide to the right here ");} # pragma mark -------- tapGesture >>> Pan pan-(void) panGesture :( UIPanGestureRecognizer *) Pan {// obtain the coordinate CGPoint point of the pan gesture on the view = [pan locationInView: self. view]; // determines the center of the view on the coordinates of the pan gesture, that is, the view is dragged to the pan Point self. mView. center = point; NSLog (@ "% @", NSStringFromCGPoint (point);} # pragma mark -------- tapGesture >>> LongPress-(void) longPressGesture :( UILongPressGestureRecognizer *) longPress {// NSLog (@ "Long press operation");} # pragma mark -------- tapGesture >>> Rotation-(void) rotationGesture :( UIRotationGestureRecognizer *) rotation {// determine the rotation size and angle of the gesture to make the view respond accordingly. float degree = rotation. rotation * (180/M_PI); NSLog (@ "rotation angle is %. 2f ", degree);} // release the created object-(void) dealloc {[_ mView release]; [super dealloc];}-(void) didReceiveMemoryWarning {[super didReceiveMemoryWarning];} @ end