• 触摸事件基本都是发生在 viewController 中,首先触摸的对象是视图,而视图的类 UIView 继承了 UIRespnder 类,但是要对事件作出处理,还需要重写 UIResponder 类中定义的事件处理函数。根据不同的触摸状态,程序会调用相应的处理函数。

1、touch 的创建

// 获取任意一个触摸对象
UITouch *touch = [touches anyObject]; // 获取任意一个触摸对象
UITouch *touch = [[event allTouches] anyObject]; // 获取指定的 view 触摸对象
UITouch *touch = [[event touchesForView:myView] anyObject]; // 获取指定的 window 触摸对象
UITouch *touch = [[event touchesForWindow:self.view.window] anyObject];

2、touch 的设置

  • 在系统触摸事件处理方法中实现
// 设置接收多点触摸
/*
默认为 NO,即视图默认不接收多点触摸
*/
self.view.multipleTouchEnabled = YES;

3、touch 的获取

  • 在系统触摸事件处理方法中实现
// 获取触摸窗口
UIWindow *touchWindow = touch.window; // 获取触摸视图
UIView *touchView = touch.view; // 获取触摸手势
NSArray *touchGesture = touch.gestureRecognizers; // 获取触摸次数
NSUInteger tapCount = touch.tapCount; // 获取触摸时间
NSTimeInterval timestamp = touch.timestamp; // 获取触摸状态
/*
UITouchPhaseBegan, // whenever a finger touches the surface. 触摸开始
UITouchPhaseMoved, // whenever a finger moves on the surface. 接触点移动
UITouchPhaseStationary, // whenever a finger is touching the surface but hasn't moved
// since the previous event. 接触点无移动
UITouchPhaseEnded, // whenever a finger leaves the surface. 触摸结束
UITouchPhaseCancelled, // whenever a touch doesn't end but we need to stop tracking
// (e.g. putting device to face) 触摸取消
*/
UITouchPhase touchPhase = touch.phase; // 获取触摸位置 // 上次触摸点的位置
CGPoint lastPoint = [touch previousLocationInView:self.view]; // 当前触摸点的位置
CGPoint currentPoint = [touch locationInView:self.view]; // 获取触摸半径
CGFloat majorRadius = touch.majorRadius;
CGFloat majorRadiusTolerance = touch.majorRadiusTolerance;

4、系统触摸事件处理方法

  • 不用手动调用
// 触摸开始,重写 UIResponder 中定义的方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { } // 触摸移动,重写 UIResponder 中定义的方法
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { } // 触摸结束,重写 UIResponder 中定义的方法
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { } // 触摸取消,重写 UIResponder 中定义的方法
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { }

5、视图随触摸移动

// 触摸移动
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { // 获取触摸对象
UITouch *touch = [touches anyObject];
UIView *tapView = touch.view; // 获取触摸点位置
CGPoint lastPoint = [touch previousLocationInView:self.view];
CGPoint currentPoint = [touch locationInView:self.view]; // 改变视图中心坐标
CGPoint tapViewCenter = tapView.center; tapViewCenter.x += currentPoint.x - lastPoint.x;
tapViewCenter.y += currentPoint.y - lastPoint.y; tapView.center = tapViewCenter;
}

6、单击/双击触摸

// 触摸结束
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; // 单击
if (touch.tapCount == 1) { // 响应单击触摸事件
[self performSelector:@selector(singleTapClick) withObject:nil afterDelay:0];
}
// 双击
else if (touch.tapCount == 2) { // 取消单击触摸响应事件
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTapClick) object:nil]; // 响应双击触摸事件
[self performSelector:@selector(doubleTapClick) withObject:nil afterDelay:0];
}
} // 单击触摸响应事件处理
- (void)singleTapClick { self.view.backgroundColor = [UIColor greenColor];
} // 双击触摸响应事件处理
- (void)doubleTapClick { self.view.backgroundColor = [UIColor orangeColor];
}

最新文章

  1. 【原】为什么选择iPhone5的分辨率作为H5视觉稿尺寸
  2. 《UML大战需求分析》阅读笔记06
  3. NPOI
  4. 【Filter 不登陆无法访问】web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面的功能
  5. 【android异步处理】一个关于android异步处理的文章系列
  6. Python之路-python(面向对象进阶(模块的动态导入、断言、Socket Server))
  7. WPF ListView 选中问题
  8. thinkphp介绍
  9. wifi reaver
  10. codevs1002搭桥(prim)
  11. LISTVIEW嵌套GRIDVIEW的一些处理(点击GRIDVIEW的条目,能够显示他在LISTVIEW中的位置)(对这篇文章的优化处理,不每次都new onItemClickListener)
  12. PC端 H5实现拍照并上传
  13. 渗透测试工具Nmap从初级到高级
  14. ToolbarDemo【Toolbar作为顶部导航栏的简单使用】
  15. Java 诊断工具 Arthas 教程学习笔记
  16. php封装curl,模拟POST和GET请求HTTPS请求
  17. 在安卓手机上安装完整kali linux系统
  18. Sql Server数据库之触发器
  19. luogu P3162 [CQOI2012]组装
  20. springboot寻找property的顺序

热门文章

  1. 第一篇 PHP开发环境搭建以及多站点配置(基于windows 7系统)
  2. nginx与二级域名的绑定 nginx安装
  3. Java-API:un-java.util.Set
  4. net.sf.json.JSONObject 和org.json.JSONObject
  5. IDEA编译器如何去掉注释中参数错误的提示
  6. 基于mjpg-streamer网络视频服务器移植
  7. 阿里云服务器ubuntu安装redis2.8.13
  8. DAY10-MYSQL表操作
  9. hadoop-2.7.3.tar.gz + spark-2.0.2-bin-hadoop2.7.tgz + zeppelin-0.6.2-incubating-bin-all.tgz(master、slave1和slave2)(博主推荐)(图文详解)
  10. [Python Study Notes]堆叠柱状图绘制