01-UIView的拖拽
1.ios當(dāng)中常用的事件分為三種:
觸摸事件
加速計(jì)事件
遠(yuǎn)程控制事件
2.什么是響應(yīng)者對(duì)象?
繼承了UIResponds的對(duì)象我們稱它為響應(yīng)者對(duì)象
UIApplication议街、UIViewController纺蛆、UIView都繼承自UIResponder
因此它們都是響應(yīng)者對(duì)象痹束,都能夠接收并處理事件
3.為什么說(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;
4.如何監(jiān)聽(tīng)UIView的觸摸事件?
想要監(jiān)聽(tīng)UIViiew的觸摸事件,首先第一步要自定義UIView,
因?yàn)橹挥袑?shí)現(xiàn)了UIResponder的事件方法才能夠監(jiān)聽(tīng)事件.
UIView的觸摸事件主要有:
一根或者多根手指開(kāi)始觸摸view所宰,系統(tǒng)會(huì)自動(dòng)調(diào)用view的下面方法.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
一根或者多根手指在view上移動(dòng)時(shí),系統(tǒng)會(huì)自動(dòng)調(diào)用view的下面方法
(隨著手指的移動(dòng)五垮,會(huì)持續(xù)調(diào)用該方法,也就是說(shuō)這個(gè)方法會(huì)調(diào)用很多次)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
一根或者多根手指離開(kāi)view扎附,系統(tǒng)會(huì)自動(dòng)調(diào)用view的下面方法
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
參數(shù)說(shuō)明:
touches:
touches中存放的都是UITouch對(duì)象,它是一個(gè)NSSet集合.
UITouch對(duì)象它就是用來(lái)保存手指相關(guān)聯(lián)的信息.包括位置,時(shí)間,階段等信息.
每一個(gè)手指對(duì)應(yīng)著一個(gè)UITouch對(duì)象.
這個(gè)UITouch是系統(tǒng)自動(dòng)幫我們創(chuàng)建的,當(dāng)手指移動(dòng)時(shí),系統(tǒng)會(huì)更新同一個(gè)UITouch對(duì)象,
使它能夠一直保存該手指在的觸摸位置
通過(guò)獲取UITouch屬性,我們可以獲得觸摸產(chǎn)生時(shí)所處的窗口,觸摸的View,時(shí)間,點(diǎn)擊的次數(shù)等,
這些都可以在通過(guò)UITouch獲取.
還可以通過(guò)UITouch提供的方法獲取當(dāng)前手指所在的點(diǎn),以及上一個(gè)手指所在的點(diǎn).
取當(dāng)前手指所在的點(diǎn)
- (CGPoint)locationInView:(UIView *)view;
獲取上一個(gè)觸摸點(diǎn)的位置.
- (CGPoint)previousLocationInView:(UIView *)view;
event:
每產(chǎn)生一個(gè)事件抠忘,就會(huì)產(chǎn)生一個(gè)UIEvent對(duì)象
UIEvent:稱為事件對(duì)象撩炊,記錄事件產(chǎn)生的時(shí)刻和類型
一次完整的觸摸過(guò)程,會(huì)經(jīng)歷3個(gè)狀態(tài):
觸摸開(kāi)始:- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
觸摸移動(dòng):- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
觸摸結(jié)束:- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
觸摸取消(可能會(huì)經(jīng)歷):- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
一次完整的觸摸過(guò)程中崎脉,只會(huì)產(chǎn)生一個(gè)事件對(duì)象拧咳,4個(gè)觸摸方法都是同一個(gè)event參數(shù)
如果兩根手指同時(shí)觸摸一個(gè)view,那么view只會(huì)調(diào)用一次touchesBegan:withEvent:方法囚灼,touches參數(shù)中裝著2個(gè)UITouch對(duì)象
如果這兩根手指一前一后分開(kāi)觸摸同一個(gè)view骆膝,那么view會(huì)分別調(diào)用2次touchesBegan:withEvent:方法,
并且每次調(diào)用時(shí)的touches參數(shù)中只包含一個(gè)UITouch對(duì)象
5.UIView拖拽思路?
1.自定義UIView,實(shí)現(xiàn)監(jiān)聽(tīng)方法.
2.確定在TouchMove方法當(dāng)中進(jìn)行操作,因?yàn)橛脩羰种冈谝晥D上移動(dòng)的時(shí)候才需要移動(dòng)視圖灶体。
3.獲取當(dāng)前手指的位置和上一個(gè)手指的位置.
4.當(dāng)前視圖的位置 = 上一次視圖的位置 + 手指的偏移量
關(guān)健代碼實(shí)現(xiàn):
拿到UITouch就能獲取當(dāng)前點(diǎn)
UITouch *touch = [touches anyObject];
獲取當(dāng)前點(diǎn)
CGPoint curP = [touch locationInView:self];
獲取上一個(gè)點(diǎn)
CGPoint preP = [touch previousLocationInView:self];
獲取手指x軸偏移量
CGFloat offsetX = curP.x - preP.x;
獲取手指y軸偏移量
CGFloat offsetY = curP.y - preP.y;
移動(dòng)當(dāng)前view
self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);