- 當(dāng)用戶觸摸屏幕時(shí), 會(huì)創(chuàng)建一個(gè)與手指相關(guān)關(guān)聯(lián)的UITouch對(duì)象
- 一個(gè)手指對(duì)應(yīng)一個(gè)UITouch對(duì)象
UITouch作用
- 保存著跟手指相關(guān)的信息,比如觸摸的位置巴元、時(shí)間毡咏、階段
- 當(dāng)手指移動(dòng)時(shí),系統(tǒng)會(huì)更新統(tǒng)一個(gè)UITouch對(duì)象逮刨,使之能夠一直保存該手指在的觸摸位置
- 當(dāng)手指離開(kāi)屏幕時(shí)呕缭,系統(tǒng)會(huì)銷毀相應(yīng)的UITouch對(duì)象
UITouch的屬性
// 觸摸產(chǎn)生時(shí)所處的窗口
@property(nonatmoic, readonly, retain) UIWindow *window;
// 觸摸產(chǎn)生時(shí)所處的視圖
@property(nonatmoic, readonly, retain) UIView *view;
// 短時(shí)間內(nèi)點(diǎn)按屏幕的次數(shù), 可以根據(jù)tapCount判斷單擊堵泽、雙擊或更多的點(diǎn)擊
@property(nonatmoic, readonly) NSUInteger tapCount;
// 記錄了觸摸事件產(chǎn)生或變化時(shí)的時(shí)間,單位是秒
@property(nonatomic, readonly) NSTimeInterval timestamp;
// 當(dāng)前觸摸事件所處的狀態(tài)
@property(nonatomic, readonly) UITouchPhase phase;
UITouch的方法
// 返回值表示觸摸在view上的位置
// 這里返回的位置是針對(duì)view的坐標(biāo)系的(以view的左上角為原點(diǎn)(0,0))
// 調(diào)用時(shí)傳入的view參數(shù)為nil的話恢总,返回的是觸摸點(diǎn)在UIWindow的位置
- (CGPoint)locationInView:(UIView *)view;
// 該方法記錄了前一個(gè)觸摸點(diǎn)的位置
- (CGPoint)previousLocationInView:(UIView *)view;
view的拖拽
// 讓當(dāng)前控件隨著手指移動(dòng)而移動(dòng)
// 獲取UITouch
UITouch *touch = [touches anyObject];
// 獲取當(dāng)前點(diǎn)
CGPoint curP = [touch locationInView:self];
// 獲取上一個(gè)點(diǎn)
CGPoint preP = [touch previousLocationInView:self];
// 計(jì)算手指x軸偏移量
CGFloat offsetX = curP.x - preP.x;
// 計(jì)算手指x軸偏移量
CGFloat offsetY = curP.y - preP.y;
// 修改控件的形變
self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);
UIEvent
- 稱為事件對(duì)象迎罗,記錄事件產(chǎn)生的時(shí)刻和類型
常見(jiàn)屬性
// 事件類型
@property(nonatomic, readonly) UIEventType type;
@property(nonatomic, readonly) UIEventSubtype subtype;
// 事件產(chǎn)生的時(shí)間
@property(nonatomic, readonly) NSTimeInterval timestamp;
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者