【问题标题】:UIScrollview getting touch eventsUIScrollview 获取触摸事件
【发布时间】:2011-07-10 03:21:54
【问题描述】:

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

【问题讨论】:

    标签: objective-c ios cocoa-touch uiscrollview uitouch


    【解决方案1】:

    设置点击手势识别器:

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

    你会接触到:

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

    【讨论】:

    • 您先生是个天才!还有所有这些关于子类化 UIScrollView 的帖子,呸!
    • 我使用了你的代码。但由于 UITapGestureRecognizer,滚动视图上的按钮单击不起作用..?
    • @Hercules 添加控制器作为识别器的委托并使用此功能: - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ if([touch.view isKindOfClass:[UIScrollView类]]) 返回是;否则返回否; }
    • @yuf 和 Hercules,请注意 iOS 5 和 6 目前区分是否在接收触摸之前计算 contentOffset。苹果决定改变这一点,并打破所有自定义代码,谁知道为什么。 5个不会,6个会。为什么要这样做!!?啊。应该帮助其他没有按钮接收输入问题的编码人员)。
    • 在 Xcode 5 IB (Interface Builder) 中将“Tap Gesture Recognizer”对象拖到 ScrollView。然后 Control 从 ScrollView 拖拽到实现代码(*.m)中添加一个点击手势方法。
    【解决方案2】:

    您可以创建自己的 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];
    }
    

    【讨论】:

      【解决方案3】:

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

      - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
      

      在方法内部,读取属性:

      @property(nonatomic) CGPoint contentOffset
      

      从scrollView获取坐标。

      【讨论】:

      • 嗨,我不想在滚动视图拖动时触摸,我需要像 TouchesBegan
      【解决方案4】:

      这也适用于着陆事件。

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

      从同一答案中 yuf 的评论中,您也可以在 UIScrollView 中从底层视图中获取 touch point。

      - (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 是否应该收到触摸。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-27
        • 1970-01-01
        相关资源
        最近更新 更多