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];
-
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
UIPanGestureRecognizer类中translationInView
原文:http://blog.csdn.net/wenhaiwang/article/details/47836645