ios UIPanGestureRecognizer 指针位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9675672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:16:09  来源:igfitidea点击:

ios UIPanGestureRecognizer pointer position

iosuipangesturerecognizer

提问by Hymansonkr

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

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

The above code will log the relative position of my current pan, but how can I get the absolute position for the view I'm in?

上面的代码将记录我当前平底锅的相对位置,但是如何获得我所在视图的绝对位置?

I'm simply just wanting to slide a UIImageViewto wherever the user's finger is.

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

回答by sch

translationInViewgives you the pan translation (how much x has changed) and not the position of the pan in the view (the value of x). If you need the position of the pan, you have to use the method locationInView.

translationInView为您提供平移平移(x 改变了多少)而不是平移在视图中的位置(x 的值)。如果你需要锅的位置,你必须使用方法locationInView

You can find the coordinates relatively to the view as follows:

您可以找到相对于视图的坐标,如下所示:

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

Or relatively to the superview:

或者相对于超级视图:

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

Or relatively to the window:

或者相对于窗口:

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

回答by budidino

Swift 5

斯威夫特 5

Use the method .location()that returns a CGPointvalue. [documentation]

使用方法.location()返回一个CGPoint值。[文档]

For example, relative location of your gesture to self.view:

例如,您的手势与 的相对位置self.view

let relativeLocation = gesture.location(self.view)
print(relativeLocation.x)
print(relativeLocation.y)

回答by Sirens

I think a simple way of something like this is to get the x and y of the touch and tracking it, once it has 2 points (say X:230 Y:122) you set the scroll of the UIScroll view to the x and y.

我认为像这样的简单方法是获取触摸的 x 和 y 并跟踪它,一旦它有 2 个点(比如 X:230 Y:122),您将 UIScroll 视图的滚动设置为 x 和 y .

I'm not sure exactly how I got to this answer... If it's not helpful don't down vote me I am still a noob D:

我不确定我是如何得到这个答案的......如果它没有帮助,请不要投票给我,我仍然是一个菜鸟 D: