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

UIPanGestureRecognizer?*pan?=?[[UIPanGestureRecognizer?alloc]?initWithTarget:self?action:@selector(handlePan:)];
[self.imgView?setUserInteractionEnabled:YES];
[self.imgView?addGestureRecognizer:pan];
[pan?release];
handlePan函數(shù)代碼如下:

-?(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