UIScrollview获取触摸事件

70 cocoa-touch objective-c uiscrollview uitouch ios

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

Sur*_*h.D 180

设置点击手势识别器:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];    
Run Code Online (Sandbox Code Playgroud)

你会得到的:

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{ 
    CGPoint touchPoint=[gesture locationInView:scrollView];
}
Run Code Online (Sandbox Code Playgroud)

  • 你先生是个天才!所有这些关于子类化UIScrollView的帖子,嗯! (6认同)
  • @Hercules将控制器添加为识别器的委托并使用此功能:-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {if([[touch.view isKindOfClass:[UIScrollView class]])返回YES;否则返回否;} (2认同)

Vic*_*cky 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];
}
Run Code Online (Sandbox Code Playgroud)