ios UIPanGestureRecognizer指针的位置

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; [self addGestureRecognizer:panRecognizer]; - (void)pan:(UIPanGestureRecognizer *)gesture { NSLog(@"%f", [gesture translationInView:self].x); } 

上面的代码会logging我当前平移的相对位置,但是我怎样才能得到我所在视图的绝对位置呢?

我只是想将UIImageView滑动到用户手指的任何位置。

translationInView给你的平移翻译(多lessx已经改变),而不是视图中的平移位置(x的值)。 如果你需要平底锅的位置,你必须使用方法locationInView

你可以find相对于视图的坐标如下:

 - (void)pan:(UIPanGestureRecognizer *)gesture { NSLog(@"%f", [gesture locationInView:self].x); } 

或者相对于超级观点:

 - (void)pan:(UIPanGestureRecognizer *)gesture { NSLog(@"%f", [gesture locationInView:self.superview].x); } 

还是相对于窗口:

 - (void)pan:(UIPanGestureRecognizer *)gesture { NSLog(@"%f", [gesture locationInView:self.window].x); } 

我认为一个简单的方法就是获得触摸的x和y,一旦它有2个点(比如说X:230 Y:122),就把UIScroll视图的滚动设置为x和y 。

我不知道我是怎么得到这个答案…如果没有帮助不投票给我我还是一个小白D:

迅速

您的手势相对于self.view位置( CGPoint ):

 print( gesture.locationInView(self.view) )