UIPanGestureRecognizer學(xué)習(xí)之translationInView方法和velocityInView

UIGestureRecognizer是一個(gè)定義基本手勢(shì)的抽象類,具體什么手勢(shì),在以下子類中包含:

1、拍擊UITapGestureRecognizer (任意次數(shù)的拍擊)

2、向里或向外捏UIPinchGestureRecognizer (用于縮放)

3、搖動(dòng)或者拖拽UIPanGestureRecognizer (拖動(dòng))

4、擦碰UISwipeGestureRecognizer (以任意方向)

5、旋轉(zhuǎn)UIRotationGestureRecognizer (手指朝相反方向移動(dòng))

6、長按UILongPressGestureRecognizer?(長按)

今天一同學(xué)問到UIPanGestureRecognizer類中translationInView方法和velocityInView方法有什么區(qū)別,因?yàn)槲乙埠镁脹]看IOS,一丟下就很難拾起,故今天研究下這個(gè)問題

UIPanGestureRecognizer主要用于拖動(dòng),比如桌面上有一張圖片uiimageview,你想讓它由原始位置拖到任何一個(gè)位置,就是圖片跟著你的手指走動(dòng),那么就需要用到該類了。

以下代碼表示給一個(gè)圖片視圖指定一個(gè)UIPanGestureRecognizer手勢(shì)當(dāng)該圖片捕獲到用戶的拖動(dòng)手勢(shì)時(shí)會(huì)調(diào)用回調(diào)函數(shù)handlePan

C代碼

UIPanGestureRecognizer?*pan?=?[[UIPanGestureRecognizer?alloc]?initWithTarget:self?action:@selector(handlePan:)];

[self.imgView?setUserInteractionEnabled:YES];

[self.imgView?addGestureRecognizer:pan];

[pan?release];

handlePan函數(shù)代碼如下:

C代碼

-?(void)?handlePan:?(UIPanGestureRecognizer?*)rec{

NSLog(@"xxoo---xxoo---xxoo");

CGPoint?point?=?[rec?translationInView:self.view];

NSLog(@"%f,%f",point.x,point.y);

rec.view.center?=?CGPointMake(rec.view.center.x?+?point.x,?rec.view.center.y?+?point.y);

[rec?setTranslation:CGPointMake(0,?0)?inView:self.view];

}

以下為本人自己的理解,有不到之處請(qǐng)看官務(wù)必指教12

- (CGPoint)translationInView:(UIView*)view方法的API解釋如下:

The translation of the pan gesture in the coordinate system of the specified view.

Return Value

A point identifying the new location of a view in the coordinate system of its designated superview.

字面理解是:

在指定的視圖坐標(biāo)系統(tǒng)中轉(zhuǎn)換(拖動(dòng)?)?pan gesture

返回參數(shù):返回一個(gè)明確的新的坐標(biāo)位置,在指定的父視圖坐標(biāo)系統(tǒng)中

簡單的理解就是

該方法返回在橫坐標(biāo)上、縱坐標(biāo)上拖動(dòng)了多少像素

因?yàn)橥蟿?dòng)起來一直是在遞增,所以每次都要用setTranslation:方法制0這樣才不至于不受控制般滑動(dòng)出視圖

- (CGPoint)velocityInView:(UIView*)view方法的API解釋如下:

The velocity of the pan gesture in the coordinate system of the specified view.

Return Value

The velocity of the pan gesture, which is expressed in points per second. The velocity is broken into horizontal and vertical components.

字面理解:

在指定坐標(biāo)系統(tǒng)中pan gesture拖動(dòng)的速度

返回參數(shù):返回這種速度

簡單的理解就是

你拖動(dòng)這個(gè)圖片的時(shí)候肯定有個(gè)速度,因此返回值就是你拖動(dòng)時(shí)X和Y軸上的速度,速度是矢量,有方向。

參考資料

http://www.cnblogs.com/andyque/archive/2011/12/30/2307060.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 羨慕別人會(huì)這個(gè)會(huì)那個(gè),以為自己什么都不會(huì),而只是從沒有花時(shí)間在這個(gè)上面,怎么可能有回報(bào),以后不要再自怨自艾,喜歡就...
    齊寶寶520閱讀 230評(píng)論 0 0
  • 讀過納蘭性德的“人生若只如初見,何事秋風(fēng)畫悲扇”這句話,愛上了納蘭多舛的命運(yùn)和優(yōu)美的詩詞。 我一直在想,此一生,我...
    86996b15573c閱讀 588評(píng)論 0 2
  • ——高三某次月考教師總結(jié)會(huì)上的講話 (以詩代講,別開生面,記憶猶新) 只有辛勤耕耘過的農(nóng)人, 才有資格享受豐收的喜...
    清水一滴閱讀 510評(píng)論 0 6

友情鏈接更多精彩內(nèi)容