手勢種類
UIGestureRecognizer : NSObject
1.- (void)addGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
附加一個手勢識別器到視圖。
一.點擊 tap
UITapGestureRecognizer : UIGestureRecognizer
1.創建一個UITapGestureRecognizer對象
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
2.numberOfTapsRequired
按幾次觸發
eg:tap.numberOfTapsRequired = 2;
3.numberOfTouchesRequired
幾根手指觸發
eg:tap.numberOfTouchesRequired = 2;
二.長按 longpress
UILongPressGestureRecognizer : UIGestureRecognizer
1.創建一個UILongPressGestureRecognizer對象
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
2.minimumPressDuration
判定長按 需要的時間 默認爲0.5s
eg:longPress.minimumPressDuration = 1;
3.allowableMovement
允許長按過程中移動的像素 默認10
eg:longPress.allowableMovement = 100;
4.常用綁定方法
if(longPress.state == UIGestureRecognizerStateBegan)
{
NSLog(@"長按");
}
三.清掃 swipe
UISwipeGestureRecognizer : UIGestureRecognizer
1.創建一個UISwipeGestureRecognizer對象
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
2.direction
滑動方向
eg:swipe.direction = UISwipeGestureRecognizerDirectionLeft;
3.numberOfTouchesRequired
幾根手指觸發
四.旋轉 rotation
UIRotationGestureRecognizer : UIGestureRecognizer
1.創建一個UIRotationGestureRecognizer對象
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
2.rotation
旋轉速度
3.常用綁定方法
NSLog(@"旋轉");
//獲得當前手勢所在的view
UIView *view = rotation.view;
//transform屬性
view.transform = CGAffineTransformRotate(view.transform, rotation.rotation);
rotation.rotation = 0;
五.拖拽 pan
UIPanGestureRecognizer : UIGestureRecognizer
1.創建一個UIPanGestureRecognizer對象
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
2.常用綁定方法
NSLog(@"拖拽");
CGPoint translation = [pan translationInView:self.view];
UIView *view = pan.view;
view.center = CGPointMake(view.center.x + translation.x, view.center.y + translation.y);
[pan setTranslation:CGPointMake(0, 0) inView:self.view];
六.捏合 pinch
UIPinchGestureRecognizer : UIGestureRecognizer
1.創建一個UIPinchGestureRecognizer對象
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
2.常用綁定方法
UIView *view = pinch.view;
view.transform = CGAffineTransformScale(view.transform, pinch.scale, pinch.scale);
3.scale
比例
eg:pinch.scale = 1;
七.屏幕邊緣拖拽 screenEdgePan
UIScreenEdgePanGestureRecognizer : UIPanGestureRecognizer
1.創建一個UIScreenEdgePanGestureRecognizer對象
UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenEdgePan:)];