UIGestureRecognizer是一個定義基本手勢的抽象類,具體什么手勢,在以下子類中包含:
1、拍擊UITapGestureRecognizer (任意次數的拍擊)
2、向里或向外捏UIPinchGestureRecognizer (用于縮放)
3、搖動或者拖拽UIPanGestureRecognizer (拖動)
4、擦碰UISwipeGestureRecognizer (以任意方向)
5、旋轉UIRotationGestureRecognizer (手指朝相反方向移動)
6、長按UILongPressGestureRecognizer?(長按)
今天一同學問到UIPanGestureRecognizer類中translationInView方法和velocityInView方法有什么區別,因為我也好久沒看IOS,一丟下就很難拾起,故今天研究下這個問題
UIPanGestureRecognizer主要用于拖動,比如桌面上有一張圖片uiimageview,你想讓它由原始位置拖到任何一個位置,就是圖片跟著你的手指走動,那么就需要用到該類了。
以下代碼表示給一個圖片視圖指定一個UIPanGestureRecognizer手勢當該圖片捕獲到用戶的拖動手勢時會調用回調函數handlePan
UIPanGestureRecognizer?*pan?=?[[UIPanGestureRecognizer?alloc]?initWithTarget:self?action:@selector(handlePan:)];
[self.imgView?setUserInteractionEnabled:YES];
[self.imgView?addGestureRecognizer:pan];
[pan?release];
handlePan函數代碼如下:
-?(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];
}
以下為本人自己的理解,有不到之處請看官務必指教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.
字面理解是:
在指定的視圖坐標系統中轉換(拖動?)?pan gesture
返回參數:返回一個明確的新的坐標位置,在指定的父視圖坐標系統中
簡單的理解就是
該方法返回在橫坐標上、縱坐標上拖動了多少像素
因為拖動起來一直是在遞增,所以每次都要用setTranslation:方法制0這樣才不至于不受控制般滑動出視圖
- (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.
字面理解:
在指定坐標系統中pan gesture拖動的速度
返回參數:返回這種速度
簡單的理解就是
你拖動這個圖片的時候肯定有個速度,因此返回值就是你拖動時X和Y軸上的速度,速度是矢量,有方向。
參考資料
http://www.cnblogs.com/andyque/archive/2011/12/30/2307060.html