UIGestureRecognizer抽象类,六大手势是其子类;
- UITapGestureRecognizer 点击
- UIPinchGestureRecognizer 缩放
- UIRotationGestureRecognizer 旋转
- UISwipeGestureRecognizer 轻扫
- UIPanGestureRecognizer 拖动
- UILongPressGestureRecognizer 长按
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {//手势当前的状态
UIGestureRecognizerStatePossible, (默认)
UIGestureRecognizerStateBegan, (开始)
UIGestureRecognizerStateChanged,(已经改变)
UIGestureRecognizerStateEnded, (结束)
UIGestureRecognizerStateCancelled, (取消)
UIGestureRecognizerStateFailed, (失败)
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
}
一、拖动手势 UIPanGestureRecognizer
UIPanGestureRecognizer *panGesture =[[UIPanGestureRecognize alloc] initWithTarget:self action:@selection(handlePan:)];
[view addGestureRecognizer:panGesture];
- (void)handlePan:(UIPanGestureRecognizer *)panGesture{ CGPoint translation = [panGesture translationInView:self.view]; CGPoint point = CGPointMake(panGesture.view.center.x + translation.x,panGesture.view.center.y + translation.y); panGesture.view.center = point; [panGesture setTranslation:CGPointZero inView:self.view]; }
1、确保view是可交互的 view.userInteractionEnabled = yes;
2、translationInView (相对点击点的偏移量) locationInView(相对屏幕实时坐标);
3、setTranslation:CGPointZero inView(每次初始化位置) 和translationInView同时使用;
4、velocityInView (获取速度矢量)
二、捏合手势 UIPinchGestureRecognizer
UIPinchGestureRecognizer *phinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget: self action:@selector(handlePinch:)];
[view addGestureRecognizer:pinchGesture];
- (void)handlePinch:(UIPinchGestureRecognizer *)recognizer{ CGFloat scale = recognizer.scale; recognizer.view.transform = CGAffineTransformMakeScale(scale, scale);// 每次缩放都回复原来基础下变化 // recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, scale, scale);//在已经缩放大小基础下累加变化 // recognizer.scale = 1.0; }
scale 缩放倍数 ,velocity 缩放速率;
三、旋转手势 UIRotationGestureRecognizer
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotaton:)];
[view addGestureRecognizer:rotationGesture];
- (void)handleRotation:(UIRotationGestureRecognizer *)recognizer { recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation); recognizer.rotation = 0.0; }
四、点击手势 UITapGestureRecognizer
@property (nonatomic) NSUInteger numberOfTapsRequired; // 点击次数(默认一次) @property (nonatomic) NSUInteger numberOfTouchesRequired __TVOS_PROHIBITED; // 需要手字数(默认一个点击点)
五、长按手势 UILongPressGestureRecognizer
@property (nonatomic) NSUInteger numberOfTapsRequired; // Default is 0. The number of full taps required before the press for gesture to be recognized @property (nonatomic) NSUInteger numberOfTouchesRequired __TVOS_PROHIBITED; // Default is 1. Number of fingers that must be held down for the gesture to be recognized @property (nonatomic) CFTimeInterval minimumPressDuration; // Default is 0.5. 最小长按时间 @property (nonatomic) CGFloat allowableMovement; // Default is 10. 长按时允许移动的距离
六、轻扫手势 UISwipeGestureRecognizer
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) { UISwipeGestureRecognizerDirectionRight = 1 << 0, UISwipeGestureRecognizerDirectionLeft = 1 << 1, UISwipeGestureRecognizerDirectionUp = 1 << 2, UISwipeGestureRecognizerDirectionDown = 1 << 3 }; @property(nonatomic) UISwipeGestureRecognizerDirection direction; //轻扫方向
号外:同时响应多种手势
设置手势delegate实现方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; }