1.UIResponder概念
a.在使用者使用app過程中,會産生各種各樣的事件,iOS中的事件可以分為3大類型:觸摸事件、加速事件、遠端控制事件。今天主要介紹觸摸事件。
b.在iOS中不是任何對象都能處理事件,隻有繼承了UIResponder的對象才能接收并處理事件,我們稱之為“響應者對象”。
c.UIApplication、UIViewController、UIView都繼承自UIResponder,是以它們都是響應者對象,都能夠接收并處理事件。
d.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;
2.UIView的觸摸事件
UIView是UIResponder的子類,可以覆寫下列4個方法處理不同的觸摸事件
//一根或者多根手指開始觸摸view,系統會自動調用view的下面方法
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event
//一根或者多根手指在view上移動,系統會自動調用view的下面方法(随着手指的移動,會持續調用該方法)
-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event
//一根或者多根手指離開view,系統會自動調用view的下面方法
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event
//觸摸結束前,某個系統事件(例如電話呼入)會打斷觸摸過程,系統會自動調用view的下面方法
-(void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent *)event
提示:touches中存放的都是UITouch對象
3.NSSet與NSArray的差別:
NSSet:
1>無序的、不重複的;
2>通過 anyObject 來通路單個元素;
3>主要通過forin周遊;
4>好處: 效率高。
5>應用場景:
*比如重用 Cell 的時候, 從緩存池中随便擷取一個就可以了, 無需按照指定順序來擷取
*當需要把資料存放到一個集合中, 然後判斷集合中是否有某個對象的時候
NSArray
1> 有序的、可以有重複對象,對象的順序是按照添加的順序來儲存的;
2> 通過下标來通路單個元素;
3> 可以通過for循環和forin來周遊;
4>好處: 有序通路。
4> 應用場景:
在絕大多數需要依賴順序的情況下(比如 tableView 的資料源集合,在實際操作中要根據下标來擷取對象)
4.補充:
a.可以通過按着option鍵和滑鼠點選實作兩點觸摸螢幕;
b.UIView預設不支援多點觸控multipleTouchEnabled,可在view的屬性中打鈎;
c.UIImageView預設不支援多點觸控multipleTouchEnabled,也不支援userInteractionEnabled可在view的屬性中打鈎;