iOS中常用的事件
觸摸事件
加速計(jì)事件
遠(yuǎn)程控制事件
什么是響應(yīng)者對(duì)象
繼承了UIResponds的對(duì)象為響應(yīng)者對(duì)象
例如:UIApplication、UIViewController、UIView都繼承自UIResponder
所以它們都是響應(yīng)者對(duì)象,都能夠接收并處理事件
為什么說(shuō)繼承了UIResponder就能夠處理事件
因?yàn)樵赨IResponder內(nèi)部提供了以下方法來(lái)處理事件
例如:觸摸事件會(huì)調(diào)用以下方法:
- (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;
加速計(jì)事件會(huì)調(diào)用:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent*)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent*)event;
遠(yuǎn)程控制事件會(huì)調(diào)用:
- (void)remoteControlReceivedWithEvent:(UIEvent*)event;
如何監(jiān)聽UIView的觸摸事件
想要監(jiān)聽UIViiew的觸摸事件,首先第一步要自定義UIView,
因?yàn)橹挥袑?shí)現(xiàn)了UIResponder的事件方法才能夠監(jiān)聽事件.
UIView的觸摸事件主要有:
手指開始觸摸view,系統(tǒng)會(huì)自動(dòng)調(diào)用:
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
手指在view上移動(dòng)時(shí),系統(tǒng)會(huì)自動(dòng)調(diào)用:
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
(在移動(dòng)過(guò)程中,會(huì)持續(xù)調(diào)用該方法,所以說(shuō)該方法會(huì)在移動(dòng)時(shí)一直調(diào)用)
手指離開view(停止觸摸),系統(tǒng)會(huì)自動(dòng)調(diào)用view的下面方法
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event