ios UIPanGestureRecognizer指针位置 [英] ios UIPanGestureRecognizer pointer position
问题描述
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?
我只是想滑动 UIImageView
到用户手指的任何地方。
I'm simply just wanting to slide a UIImageView
to wherever the user's finger is.
推荐答案
translationInView
为您提供平移(x已经改变了多少)而不是视图中平移的位置(x的值)。如果你需要平移的位置,你必须使用方法 locationInView
。
translationInView
gives 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
.
你可以找到坐标相对于以下观点:
You can find the coordinates relatively to the view as follows:
- (void)pan:(UIPanGestureRecognizer *)gesture {
NSLog(@"%f", [gesture locationInView:self].x);
}
或相对于superview:
Or relatively to the superview:
- (void)pan:(UIPanGestureRecognizer *)gesture {
NSLog(@"%f", [gesture locationInView:self.superview].x);
}
或相对于窗口:
- (void)pan:(UIPanGestureRecognizer *)gesture {
NSLog(@"%f", [gesture locationInView:self.window].x);
}
这篇关于ios UIPanGestureRecognizer指针位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!