iOS中的触摸事件

本文深入探讨了iOS开发中响应者对象的概念,详细解释了如何通过继承UIResponder来接收并处理触摸、加速计和远程控制事件。重点介绍了UIResponder内部提供的关键方法及触摸事件的处理流程,包括触摸开始、移动、结束和取消,以及触摸事件相关属性的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


 •在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件。我们称之为“响应者对象

 •UIApplicationUIViewControllerUIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件"

UIResponder内部提供了以下方法来处理事件
触摸事件

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event;

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event;

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event;

- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event;

加速计事件

- (void)motionBegan:(UIEventSubtype)motionwithEvent:(UIEvent *)event;

- (void)motionEnded:(UIEventSubtype)motionwithEvent:(UIEvent *)event;

- (void)motionCancelled:(UIEventSubtype)motionwithEvent:(UIEvent *)event;

远程控制事件

- (void)remoteControlReceivedWithEvent:(UIEvent *)event;

提示:touches中存放的都是UITouch对象

touches属性:

•触摸产生时所处的窗口

@property(nonatomic,readonly,retain) UIWindow    *window;

•触摸产生时所处的视图

@property(nonatomic,readonly,retain) UIView      *view;

•短时间内点按屏幕的次数,可以根据tapCount判断单击、双击或更多的点击

@property(nonatomic,readonly) NSUInteger          tapCount;

•记录了触摸事件产生或变化时的时间,单位是秒

@property(nonatomic,readonly) NSTimeInterval      timestamp;

•当前触摸事件所处的状态

@property(nonatomic,readonly) UITouchPhase        phase;

touches和event参数:

•一次完整的触摸过程,会经历3个状态:
触摸开始:- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event
触摸移动:- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
触摸结束:- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
触摸取消(可能会经历):- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event
•4个触摸事件处理方法中,都有NSSet*touches和UIEvent*event两个参数
一次完整的触摸过程中,只会产生一个事件对象,4个触摸方法都是同一个event参数
如果两根手指同时触摸一个view,那么view只会调用一次touchesBegan:withEvent:方法,touches参数中装着2个UITouch对象
如果这两根手指一前一后分开触摸同一个view,那么view会分别调用2次touchesBegan:withEvent:方法,并且每次调用时的touches参数中只包含一个UITouch对象
根据touches中UITouch的个数可以判断出是单点触摸还是多点触摸


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值