UIGestureRecognizer

UIGestureRecognizer类,用于检测、识别用户使用设备时所用的手势.它是一个抽象类,定义了所有手势的基本行为.以下是UIGestureRecognizer子类,用于处理具体的用户手势行为:

UITapGestureRecognizer // 1.单击

UILongPressGestureRecognizer // 3.长按

UISwipeGestureRecognizer // 4.轻扫

UIPanGestureRecognizer // 5.移动

UIRotationGestureRecognizer // 6.旋转

UIPinchGestureRecognizer // 7.捏合

创建手势:
    // 1.单击
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[imgView addGestureRecognizer:tap]; // 2.双击
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];
doubleTap.numberOfTapsRequired = 2;
[imgView addGestureRecognizer:doubleTap]; // 双击失败才单击
[tap requireGestureRecognizerToFail:doubleTap]; // 3.长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
//设置最短时间
longPress.minimumPressDuration = 1;
[imgView addGestureRecognizer:longPress]; // 4.轻扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
// 设置轻扫方向
[swipe setDirection:UISwipeGestureRecognizerDirectionRight];
[imgView addGestureRecognizer:swipe]; // 5.移动
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[imgView addGestureRecognizer:pan]; // 轻扫失败才移动
[pan requireGestureRecognizerToFail:swipe]; // 6.旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[imgView addGestureRecognizer:rotation]; // 7.捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imgView addGestureRecognizer:pinch];
手势触发事件:

GestureAction:

-(void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按开始");
}else if (longPress.state == UIGestureRecognizerStateEnded){
NSLog(@"长按结束");
}
} - (void)panAction:(UIPanGestureRecognizer *)pan { //手指所在的坐标
CGPoint point = [pan locationInView:self.view];
_view.center = point;
} - (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{ if (rotation.state == UIGestureRecognizerStateChanged) { //取到弧度
CGFloat angle = rotation.rotation; //正在旋转
rotation.view.transform = CGAffineTransformMakeRotation(angle); } else if (rotation.state == UIGestureRecognizerStateEnded) { //还原
[UIView animateWithDuration:.5 animations:^{ rotation.view.transform = CGAffineTransformIdentity;
}];
}
} - (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{ if (pinch.state == UIGestureRecognizerStateChanged) { // 取到缩放比率
CGFloat scale = pinch.scale; // 缩放
pinch.view.transform = CGAffineTransformMakeScale(scale, scale); } else if (pinch.state == UIGestureRecognizerStateEnded) { [UIView animateWithDuration:.5 animations:^{ pinch.view.transform = CGAffineTransformIdentity;
}];
}
}
Motion 摇晃手势
//让当前对象成为第一响应者
- (BOOL)canBecomeFirstResponder
{ return YES;
} - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{ NSLog(@"摇一摇开始");
} - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{ NSLog(@"摇一摇结束");
}

推荐一篇iOS手势识别的详细使用的文章:iOS手势识别


最新文章

  1. Java实现时间动态显示方法汇总
  2. thread_fork/join并发框架2
  3. fis-receiver:一行命令将项目部署到远程服务器
  4. 加快SQL连接速度
  5. leetcode解题—Longest Palindromic Substring
  6. spring-quartz普通任务与可传参任务
  7. maven在mac上的入门使用
  8. android音乐柱状频谱实现
  9. CUMCM--总结
  10. 【高德地图API】如何制作自己的旅游地图?
  11. 【Egret】中tree组件使用案例
  12. Python os.walk的用法与举例
  13. 设计模式-建造者模式(Builder)
  14. python小程序--Three(三级菜单)
  15. 解决在编程方式下无法访问Spark Master问题
  16. Java 作业6
  17. centos7 LANMP 安装
  18. eval 用法
  19. url下载文件到本地
  20. stl源码剖析 详细学习笔记deque(2)

热门文章

  1. JAVA程序编写入门
  2. 4 关于word2vec的skip-gram模型使用负例采样nce_loss损失函数的源码剖析
  3. 排序算法积累(2)----sort排序
  4. jquery attr和prop区别
  5. dubbo学习总结三 消费端
  6. java几种基本排序算法
  7. bitekind
  8. Linux下安装方法总结(源码安装)
  9. SAP Cloud for Customer里Sales Order和Sales Quote的建模方式
  10. [原]如何在Android用FFmpeg+SDL2.0之同步音频