72

如何检测我的触摸点UIScrollView?触摸委托方法不起作用。

4

4 回答 4

181

设置点击手势识别器:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];    

你会接触到:

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{ 
    CGPoint touchPoint=[gesture locationInView:scrollView];
}
于 2011-03-07T06:22:28.993 回答
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];
}
于 2012-05-15T10:33:04.020 回答
1

如果我们谈论的是滚动视图中的点,那么您可以使用委托方法:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

在方法内部,读取属性:

@property(nonatomic) CGPoint contentOffset

从 scrollView 获得协调。

于 2011-03-07T06:12:39.087 回答
0

这也适用于触地事件。

在当前标记为正确的答案中,您touch point只能在“点击”事件中获得。这个事件似乎只在“手指向上”而不是向下触发。

从同一答案中 yuf 的评论中,您也可以touch pointUIScrollView.

- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch
{
  CGPoint touchPoint = [touch locationInView:self.view];

  return TRUE; // since we're only interested in the touchPoint
}

根据Apple的文档gestureRecognizer确实:

询问代表手势识别器是否应该接收表示触摸的对象。

这对我来说意味着我可以决定是否gestureRecognizer应该接受触摸。

于 2019-09-25T07:25:54.643 回答