UIScrollview获得触摸事件


Answers:


180

设置点击手势识别器:

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

您将得到以下方面的信息:

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{ 
    CGPoint touchPoint=[gesture locationInView:scrollView];
}

6
先生,你真是个天才!还有所有有关将UIScrollView子类化的文章,posts!
奥利弗·皮尔曼

1
我用了你的代码。但是由于UITapGestureRecognizer,在滚动视图上单击按钮不起作用。
Ankur 2012年

2
@Hercules将控制器添加为识别器的委托并使用此功能:-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {if([[touch.view isKindOfClass:[UIScrollView class]])返回YES;否则返回否;}
yuf 2012年

@yuf和Hercules,请注意,iOS 5和iOS 6当前在接收触摸之前是否对contentOffset进行了区分。Apple决定更改此设置,并为知道原因的人打破所有自定义代码。5否,6则。为什么这样做!?啊 应该可以帮助其他没有按钮的编码器接收输入问题)。
斯蒂芬·J

在Xcode 5 IB(界面生成器)中,将“点击手势识别器”(Tap Gesture Recognizer)对象拖到ScrollView。然后将Control从ScrollView拖动到实现代码(* .m),以添加轻击手势方法。
大卫·道格拉斯

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];
}

1

如果我们要讨论的是scrollview中的点,则可以使用委托方法进行挂钩:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

在方法内部,读取属性:

@property(nonatomic) CGPoint contentOffset

从scrollView获得协调。


2
嗨,我不想在scrollview拖动时得到触摸,我需要像TouchesBegan

0

这也适用于触地事件。

在当前正确标记的答案中,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的文档gestureRecognizer可以:

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

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.