2012-03-12 72 views
16
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; 
[self addGestureRecognizer:panRecognizer]; 

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

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

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

+0

你说的* *“为我在视图中的绝对位置”是什么意思? – sch 2012-03-12 22:27:19

回答

27

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

您可以比较找到坐标的观点如下:

- (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); 
} 
+7

使用'UIGestureRecognizer'的'view'属性代替'self'会更好吗?它可以让你移动手势识别器而不会破坏事物。对于窗口位置,通过'nil'就足够了。 – 2013-03-28 14:49:49

0

我认为一个简单的方法就是获得触摸的x和y值,并且一旦它有2点(s ay X:230 Y:122)您将UIScroll视图的滚动设置为x和y。

我不知道我究竟是如何得到这个答案......如果不是有帮助不下来投我,我还是一个小白d:

0

斯威夫特

相对位置(你的姿态CGPoint)至self.view

print(gesture.locationInView(self.view))