• 触摸事件基本都是发生在 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];
}

UITouch的更多相关文章

  1. 你真的了解UIEvent、UITouch吗?

    一:首先查看一下关于UIEvent的定义 //事件类型 typedef NS_ENUM(NSInteger, UIEventType) { UIEventTypeTouches, UIEventTyp ...

  2. iOS开发——UI进阶篇(十二)事件处理,触摸事件,UITouch,UIEvent,响应者链条,手势识别

    触摸事件 在用户使用app过程中,会产生各种各样的事件 一.iOS中的事件可以分为3大类型 触摸事件加速计事件远程控制事件 响应者对象在iOS中不是任何对象都能处理事件,只有继承了UIResponde ...

  3. iOS - UITouch

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITouch : NSObject @available(iOS 2.0, *) public class UIT ...

  4. UITouch的用法

    UITouch一般无法直接获取,是通过UIView的touchesBegan等函数获得. //这四个方法是UIResponder中得方法 // Generally, all responders wh ...

  5. 触摸事件UITouch的用法

    触摸屏幕是iOS设备接受用户输入的主要方式,包括单击.双击.拨动以及多点触摸等,这些操作都会产生触摸事件. 在Cocoa中,代表触摸对象的类是UITouch.当用户触摸屏幕后,就会产生相应的事件,所有 ...

  6. UITouch 触摸事件处理(实例)

    来源:http://www.open-open.com/lib/view/open1341882439838.html 1. UITouch 的主要方法: - (void)touchesBegan:( ...

  7. UITouch触摸事件

    UITouch触摸事件 主要为三个方法 1.-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{2.3. UITouch * ...

  8. UITouch附加

    框架 /System/Library/Frameworks/SpriteKit.framework 可用性 可用于iOS 7.0或者更晚的版本 声明于 SKNode.h 概览 重要提示:这是一个初步的 ...

  9. UITouch - BNR

    本节任务:创建一个视图,让用户在视图上拖动手指来画线. UIView类能够重载4个方法来处理不同的触摸事件. - (void)touchesBegan:(NSSet *)touches withEve ...

  10. 触摸事件,手势识别(UITouch,UIGestureRecognizer)

    触摸发生时,UIWindow会有一个队列来存放所有的触摸事件,然后再把这些事件发送给对应的hit-test view,hit-test view会通过touch的四个函数来接收这些事件. 四个函数分别 ...

随机推荐

  1. 给JavaScript文件传入参数的几种方法

    一.利用全局变量 这是最简单的一种方式,比如Google Adsense: <script type="text/javascript"> google_ad_clie ...

  2. request_mem_region,ioremap 和phys_to_virt()

    转载: request_mem_region,ioremap 和phys_to_virt()   Linux在头文件include/linux/ioport.h中定义了三个对I/O内存资源进行操作的宏 ...

  3. [转]ubuntu 网络配置 作者:Yudar

    检查网络配置命令:ifconfig 一.通过配置文件配置 新手没怎么用过Ubuntu,所以走了不少弯路,网上找了很多方法,大都没对我起到帮助作用,所以把自己的配置方法写一写. Ubuntu上连了两块网 ...

  4. Android SDK下载项的说明

    Tools下 1.android sdk tools 软件开发工具包(software development kit):包括测试.调试.第三方工具.模拟器.数据管理工具等. 2.android sd ...

  5. 2015.3.3 VC++6制作MFC dll并在其中使用对话框、引入类的操作

    上例建立的dll为非MFC的,不能使用MFC框架,如CString.对话框等类型,使用起来有一定限制.可以建立MFC的Dll来改进.建立MFC Dll的方法: 1.在VC6中新建工程时选择:MFC A ...

  6. SpringBoot外部配置

    Spring Boot的配置文件 Spring Boot使用一个全局的配置文件application.properties或者application.yml(yaml语言的配置文件),放置在src/m ...

  7. CentOS6.5安装完没有网络的解决办法

    昨天下了个CentOS 6.5 Minimal 版,在VMware 10下安装好了之后,发现上不了网,PING外网也PING不通. 在网上搜了一下,发现Linux安装好了之后,网卡默认是没有启动的,下 ...

  8. elasticsearch(4) 安装 (两台)

    环境: centos7  jdk8   elasticsearch1.7.1 安装JDK 确认现有JDK版本 # java –version 安装以及配置环境变量 # tar zxvf jdk-8u6 ...

  9. 解决Xcode在debug时不在断点处停止的方法<转>

    搞了老半天不知道为什么 后来查了一下才解决问题,多谢原创作者的贡献. 新年后的第一发! -------------------------------- 前几天在开发的时候,Xcode设置断点后依然无 ...

  10. 我的第一个Socket程序-SuperSocket使用入门(二)

    操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操操 辛辛苦苦写那么久的博客,最后手贱点了全屏富文本编辑器 ...