对于不一样类型的手势识别器,具备不一样的配置属性。好比UITapGestureRecognizer,能够配置拍击次数。界面接收到手势以后,能够发送一个消息,用于处理响应手势动做后的任务。固然,不一样的手势识别器,发送的消息方法也会有所不一样。ui
// 建立一个手势识别器 UITapGestureRecognizer *oneFingerTwoTaps = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease]; // Set required taps and number of touches [oneFingerTwoTaps setNumberOfTapsRequired:2]; [oneFingerTwoTaps setNumberOfTouchesRequired:1]; // Add the gesture to the view [[self view] addGestureRecognizer:oneFingerTwoTaps]; 消息方法oneFingerTwoTaps - (void)oneFingerTwoTaps { NSLog(@"Action: One finger, two taps"); }
UITapGestureRecognizer *twoFingersTwoTaps = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease]; [twoFingersTwoTaps setNumberOfTapsRequired:2]; [twoFingersTwoTaps setNumberOfTouchesRequired:2]; [[self view] addGestureRecognizer:twoFingersTwoTaps]; 消息方法twoFingersTwoTaps - (void)twoFingersTwoTaps { NSLog(@"Action: Two fingers, two taps"); }
// 向上擦碰 UISwipeGestureRecognizer *oneFingerSwipeUp = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease]; [oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp]; [[self view] addGestureRecognizer:oneFingerSwipeUp]; - (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer { CGPoint point = [recognizer locationInView:[self view]]; NSLog(@"Swipe up - start location: %f,%f", point.x, point.y); } // 向下擦碰 UISwipeGestureRecognizer *oneFingerSwipeDown = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeDown:)] autorelease]; [oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown]; [[self view] addGestureRecognizer:oneFingerSwipeDown]; - (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer { CGPoint point = [recognizer locationInView:[self view]]; NSLog(@"Swipe down - start location: %f,%f", point.x, point.y); }
UIRotationGestureRecognizer *twoFingersRotate = [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)] autorelease]; [[self view] addGestureRecognizer:twoFingersRotate]; - (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer { // Convert the radian value to show the degree of rotation NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI)); }
UIPinchGestureRecognizer *twoFingerPinch = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease]; [[self view] addGestureRecognizer:twoFingerPinch]; - (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer { NSLog(@"Pinch scale: %f", recognizer.scale); }