UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self addGestureRecognizer:panRecognizer];
- (void)pan:(UIPanGestureRecognizer *)gesture {
NSLog(@"%f", [gesture translationInView:self].x);
}上面的代码将记录我当前平移的相对位置,但是我如何才能获得我所在视图的绝对位置?
我只是想滑动一个UIImageView到用户手指所在的位置。
发布于 2012-03-13 06:30:29
translationInView提供平移平移(x的变化量),而不是平移在视图中的位置(x的值)。如果需要平移的位置,则必须使用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);
}发布于 2017-01-20 13:04:52
Swift 5
使用返回CGPoint值的方法.location()。[documentation]
例如,手势相对于self.view的相对位置
let relativeLocation = gesture.location(self.view)
print(relativeLocation.x)
print(relativeLocation.y)发布于 2012-03-13 06:40:55
我认为这样做的一个简单方法是获取触摸的x和y并跟踪它,一旦它有2个点(比如X:230 Y:122),就可以将UIScroll视图的滚动设置为x和y。
我不确定我是怎么得到这个答案的。如果没有帮助,请不要否定我,我仍然是一个菜鸟。
https://stackoverflow.com/questions/9675672
复制相似问题