【问题标题】:ios UIPanGestureRecognizer pointer positionios UIPanGestureRecognizer 指针位置
【发布时间】:2012-03-12 22:25:16
【问题描述】:
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self addGestureRecognizer:panRecognizer];

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

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

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

【问题讨论】:

  • “我所在的视图的绝对位置”是什么意思

标签: ios uipangesturerecognizer


【解决方案1】:

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

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

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

或者相对于superview:

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

或者相对于窗口:

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

【讨论】:

  • UIGestureRecognizerview属性代替self不是更好吗?它允许您在不破坏任何东西的情况下移动手势识别器。同样对于窗口位置,传递nil就足够了。
【解决方案2】:

斯威夫特 5

使用返回CGPoint 值的方法.location()。 [documentation]

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

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

【讨论】:

  • 谢谢!可以发布正确的调用以在窗口或视图中获取绝对/非相对位置吗?提前致谢!
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 2018-10-22
    • 2019-07-15
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    相关资源
    最近更新 更多