【发布时间】:2011-12-16 19:50:17
【问题描述】:
是否可以检测到手指在 UIScrollView 中触摸的位置?
我的意思是,假设用户以这种方式使用他的手指:点击并滚动,再次抬起手指,点击和滚动等。是否有可能知道与 self.view 相关的点击发生的 CGPoint卷轴在?滚动条占据了整个 self.view。
谢谢。
【问题讨论】:
标签: iphone ios cocoa-touch ipad uiscrollview
是否可以检测到手指在 UIScrollView 中触摸的位置?
我的意思是,假设用户以这种方式使用他的手指:点击并滚动,再次抬起手指,点击和滚动等。是否有可能知道与 self.view 相关的点击发生的 CGPoint卷轴在?滚动条占据了整个 self.view。
谢谢。
【问题讨论】:
标签: iphone ios cocoa-touch ipad uiscrollview
您可以使用手势识别器来做到这一点。要检测单击位置,请使用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,您可以在 UIView 类中使用 convertPoint:toView: 方法
【讨论】:
UIkit.framework,但我找不到UITapGestureRecognizer。当我尝试写 UITapGestureRecognizer 时,它给了我未找到的错误。
您可以在视图中找到位置并将滚动设置添加到它。现在,您的下一个问题是不会调用-(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);
}
【讨论】:
【讨论】:
看看touchesBegan:withEvent:,你会得到一个UITouch's的NSSet,一个UITouch包含一个locationInView:方法,它应该返回触摸的CGPoint。
【讨论】: