UIGestureRecognizer是一個抽象類,定義了所有手勢的基本行為,它有6個子類處理具體的手勢:
1.UITapGestureRecognizer (任意手指任意次數的點擊)
// 點擊次數
numberOfTapsRequired
// 手指個數
numberOfTouchesRequired
- UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] init];
- [tapGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
- [tapGestureRecognizer setNumberOfTapsRequired:2];
- [tapGestureRecognizer setNumberOfTouchesRequired:2];
- [self.view addGestureRecognizer:tapGestureRecognizer];
- [tapGestureRecognizer release];
2.UIPinchGestureRecognizer (兩個手指捏合動作)
// 手指捏合,大於1表示兩個手指之間的距離變大,小於1表示兩個手指之間的距離變小
scale
// 手指捏合動作時的速率(加速度)
velocity
- UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] init];
- [pinchGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
- [self.view addGestureRecognizer:pinchGestureRecognizer];
- [pinchGestureRecognizer release];
3.UIPanGestureRecognizer (搖動或者拖拽)
// 最少手指個數
minimumNumberOfTouches
// 最多手指個數
maximumNumberOfTouches
- UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] init];
- [panGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
- [panGestureRecognizer setMinimumNumberOfTouches:1];
- [panGestureRecognizer setMaximumNumberOfTouches:5];
- [self.view addGestureRecognizer:panGestureRecognizer];
- [panGestureRecognizer release];
4.UISwipeGestureRecognizer (手指在屏幕上滑動操作手勢)
// 滑動手指的個數
numberOfTouchesRequired
// 手指滑動的方向 (Up,Down,Left,Right)
direction
- UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] init];
- [swipeGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
- [swipeGestureRecognizer setNumberOfTouchesRequired:2];
- [swipeGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];
- [self.view addGestureRecognizer:swipeGestureRecognizer];
- [swipeGestureRecognizer release];
5.UIRotationGestureRecognizer (手指在屏幕上旋轉操作)
// 旋轉方向,小於0為逆時針旋轉手勢,大於0為順時針手勢
rotation
// 旋轉速率
velocity
- UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] init];
- [rotationGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
- [self.view addGestureRecognizer:rotationGestureRecognizer];
- [rotationGestureRecognizer release];
6.UILongPressGestureRecognizer (長按手勢)
// Default is 0. The number of full taps required before the press for gesture to be recognized
numberOfTapsRequired
// 需要長按的手指的個數
numberOfTouchesRequired
// 需要長按的時間,最小為0.5s
minimumPressDuration
// 手指按住允許移動的距離
allowableMovement
- UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
- [longPressGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
- [longPressGestureRecognizer setMinimumPressDuration:1.0f];
- [longPressGestureRecognizer setAllowableMovement:50.0];
- [self.view addGestureRecognizer:longPressGestureRecognizer];
- [longPressGestureRecognizer release];