iPhone - UIScrollView...检测触摸的坐标

发布于 2024-12-12 02:04:35 字数 338 浏览 0 评论 0原文

可能的重复:
UIScrollview获取触摸事件

是否可以检测手指触摸 UIScrollView 中的位置?

我的意思是,假设用户以这种方式使用他的手指:点击并滚动,再次抬起手指,点击并滚动等。是否有可能知道相对于 self.view 滚动条发生点击的 CGPoint在?滚动条占据了整个 self.view。

谢谢。

Possible Duplicate:
UIScrollview getting touch events

Is it possible to detect where in a UIScrollView the finger touched?

I mean, suppose the user uses his finger in this way: taps and scroll, lifts the finger and again, taps and scroll, etc. Is it possible to know the CGPoint where the taps happened in relation to the self.view the scroller is in? The scroller occupies the whole self.view.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫竹語嫣☆ 2024-12-19 02:04:35

您可以使用手势识别器来做到这一点。要检测单击位置,请使用 UITapGestureRecognizer

UITapGestureRecognizer *tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)] autorelease];
[myScrollView addGestureRecognizer:tapRecognizer];

- (void)tapAction:(UITapGestureRecognizer*)sender{ 
    CGPoint tapPoint = [sender locationInView:myScrollView];
    CGPoint tapPointInView = [myScrollView convertPoint:tapPoint toView:self.view];
}

要将 tapPoint 转换为 self.view,您可以使用 convertPoint:toView: 方法

You can do it with gesture recognizers. For detect single tap location use UITapGestureRecognizer

UITapGestureRecognizer *tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)] autorelease];
[myScrollView addGestureRecognizer:tapRecognizer];

- (void)tapAction:(UITapGestureRecognizer*)sender{ 
    CGPoint tapPoint = [sender locationInView:myScrollView];
    CGPoint tapPointInView = [myScrollView convertPoint:tapPoint toView:self.view];
}

To convert that tapPoint to self.view you can use convertPoint:toView: method in UIView class

只是偏爱你 2024-12-19 02:04:35

Take a look at touchesBegan:withEvent: You will get a NSSet of UITouch's, and a UITouch contains a locationInView: method that should return the CGPoint of the touch.

智商已欠费 2024-12-19 02:04:35

您可以在视图中找到该位置并向其添加滚动偏移。现在,您的下一个问题是 -(void)touchesBegan:touches:event 将不会被调用,因为事件将发送到您的滚动视图。这可以通过对 UIScrollView 进行子类化并让滚动视图将触摸事件发送到下一个响应者(您的视图)来解决。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // Position of touch in view
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];

    // Scroll view offset
    CGPoint offset = scrollView.contentOffset;

    // Result
    CGPoint scrollViewPoint = CGPointMake(touchPoint.x, touchPoint.y + offset.y);
    NSLog(@"Touch position in scroll view: %f %f", scrollViewPoint.x, scrollViewPoint.y);
}

You could find the location in the view and add the scroll ofset to it. Now, your next problem is that -(void)touchesBegan:touches:event won't be called because the events will be sent to your scrollview. This can be fixed by subclassing your UIScrollView and have the scrollview send the touch events to the next responder (your view).

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // Position of touch in view
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];

    // Scroll view offset
    CGPoint offset = scrollView.contentOffset;

    // Result
    CGPoint scrollViewPoint = CGPointMake(touchPoint.x, touchPoint.y + offset.y);
    NSLog(@"Touch position in scroll view: %f %f", scrollViewPoint.x, scrollViewPoint.y);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文