iOS事件处理

iOS中常用的事件

触摸事件

加速计事件

远程控制事件

什么是响应者对象

继承了UIResponds的对象为响应者对象

例如:UIApplication、UIViewController、UIView都继承自UIResponder

所以它们都是响应者对象,都能够接收并处理事件

为什么说继承了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)motion withEvent:(UIEvent*)event;

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent*)event;

远程控制事件会调用:

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

如何监听UIView的触摸事件

想要监听UIViiew的触摸事件,首先第一步要自定义UIView,

因为只有实现了UIResponder的事件方法才能够监听事件.

UIView的触摸事件主要有:

手指开始触摸view,系统会自动调用:

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

手指在view上移动时,系统会自动调用:

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

(在移动过程中,会持续调用该方法,所以说该方法会在移动时一直调用)

手指离开view(停止触摸),系统会自动调用view的下面方法

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

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容