手势操作
Posted LLLLLuke
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手势操作相关的知识,希望对你有一定的参考价值。
1.创建手势识别的实例
2.设置手势识别器的属性
3.将手势识别添加附加到指定的视图上
4.编写监听的方法
手势的状态:
recognizer.state
{
// 常用的四个:
UIGestureRecognizerStateBegan // 开始
UIGestureRecognizerStateChanged// 改变
UIGestureRecognizerStateEnded // 结束
UIGestureRecognizerStateCancelled// 取消
}
获取这个状态时坐标:
CGPoint loc=[recognizer locationInView:self];
点按: #pragma mark - 点按 - (IBAction)tapBtnClick { // 创建手势的实例 UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)]; // 设置属性: // 敲几下 tap.numberOfTapsRequired=2; tap.numberOfTouchesRequired=2; // 几个手指敲 // 添加到这个view中 [self.myView addGestureRecognizer:tap]; } // 实现方法 - (void)tapAction:(UIGestureRecognizer *)recognizer{ NSLog(@"我被点按了"); }
长按: #pragma mark - 长按 - (IBAction)longPressBtnClick:(id)sender { UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)]; [self.myView addGestureRecognizer:longPress]; } - (void)longPressAction:(UIGestureRecognizer *)recognizer{ // 要设置他只执行一次,因为在这个时候是会有小bug的 if (recognizer.state!=UIGestureRecognizerStateBegan) { return; } NSLog(@"被长按了"); }
缩放:
- (IBAction)scaleBtnClick:(id)sender { UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)]; pinch.delegate=self; [self.myView addGestureRecognizer:pinch]; } - (void)pinchAction:(UIPinchGestureRecognizer *)recognizer{ // 咱们是去去缩放这个view 就是去改变这个的transform的属性 CGFloat scale= recognizer.scale; self.myView.transform=CGAffineTransformScale(self.myView.transform, scale, scale); recognizer.scale=1; }
拖拽: #pragma mark - 拖拽 - (IBAction)panBtnClick:(id)sender { UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)]; [self.myView addGestureRecognizer:pan]; } - (void )panAction:(UIPanGestureRecognizer *)recognizer{ // 拖拽也是去改变它的transform // 1.获取移动的距离 CGPoint distance=[recognizer translationInView:self.view]; //2.移动图片框 self.myView.transform=CGAffineTransformTranslate(self.myView.transform, distance.x, distance.y); //3.清除移动距离 [recognizer setTranslation:CGPointZero inView:self.view]; }
旋转 #pragma mark - 旋转 - (IBAction)rotaionBtnClick:(id)sender { UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotaionAction:)]; rotation.delegate=self; [self.myView addGestureRecognizer:rotation]; } - (void)rotaionAction:(UIRotationGestureRecognizer *)recognizer{ CGFloat angle=recognizer.rotation; self.myView.transform=CGAffineTransformRotate(self.myView.transform, angle); recognizer.rotation=0; }
轻扫: #pragma mark - 轻扫 - (IBAction)swipBtnclick:(id)sender { UISwipeGestureRecognizer *swip1=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)]; // swip1.description=UISwipeGestureRecognizerDirectionRight; [self.myView addGestureRecognizer:swip1]; UISwipeGestureRecognizer *swip2=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)]; swip2.direction=UISwipeGestureRecognizerDirectionLeft; [self.myView addGestureRecognizer:swip2]; } - (void)swipAction:(UISwipeGestureRecognizer *)recognizer{ // NSLog(@"轻扫"); // 根据方向进行判断 if (recognizer.direction==UISwipeGestureRecognizerDirectionLeft) { // 向左 self.myView.transform=CGAffineTransformTranslate(self.myView.transform, -200, 0); // 再通过动画返回 [UIView animateWithDuration:0.5 animations:^{ self.myView.transform=CGAffineTransformIdentity ; }]; }else{ // 向右 self.myView.transform=CGAffineTransformTranslate(self.myView.transform, 200, 0); // 再通过动画返回 [UIView animateWithDuration:0.5 animations:^{ self.myView.transform=CGAffineTransformIdentity ; }]; } }
多手势操作: #pragma mark - 多手势识别 三步 /* // 创建代理 // 遵循协议 //实现方法 */ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; }
以上是关于手势操作的主要内容,如果未能解决你的问题,请参考以下文章
VSCode自定义代码片段15——git命令操作一个完整流程