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