如何检测UIScrollView
中的触摸点?触摸委托方法不起作用。
答案 0 :(得分:180)
设置点击手势识别器:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];
您将接触到:
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
CGPoint touchPoint=[gesture locationInView:scrollView];
}
答案 1 :(得分:6)
您可以创建自己的UIScrollview子类,然后可以实现以下内容:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"DEBUG: Touches began" );
UITouch *touch = [[event allTouches] anyObject];
[super touchesBegan:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"DEBUG: Touches cancelled");
// Will be called if something happens - like the phone rings
UITouch *touch = [[event allTouches] anyObject];
[super touchesCancelled:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"DEBUG: Touches moved" );
UITouch *touch = [[event allTouches] anyObject];
[super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"DEBUG: Touches ending" );
//Get all the touches.
NSSet *allTouches = [event allTouches];
//Number of touches on the screen
switch ([allTouches count])
{
case 1:
{
//Get the first touch.
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
switch([touch tapCount])
{
case 1://Single tap
break;
case 2://Double tap.
break;
}
}
break;
}
[super touchesEnded:touches withEvent:event];
}
答案 2 :(得分:1)
如果我们在讨论scrollview中的点,那么你可以使用委托方法挂钩:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
并在方法内部,阅读属性:
@property(nonatomic) CGPoint contentOffset
从scrollView获得协调。
答案 3 :(得分:0)
这也适用于触地事件。
在当前正确标记的answer中,您只能在“点击”事件中获得touch point
。此事件似乎只会在“手指向上”上触发,而不是向下。
根据yuf在同一答案中的评论,您还可以在touch point
中从基础视图中获得UIScrollView
。
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch
{
CGPoint touchPoint = [touch locationInView:self.view];
return TRUE; // since we're only interested in the touchPoint
}
根据Apple的documentation,gestureRecognizer
会做到:
询问代表是否手势识别器应该接收到代表触摸的对象。
对我来说意味着我可以决定gestureRecognizer
是否应该接受触摸。