【问题标题】:Detect touch position on UiScrollView检测 UiScrollView 上的触摸位置
【发布时间】:2013-03-23 15:30:53
【问题描述】:

我需要从 UIScrollView 获取触摸位置。但是当我为我的滚动视图创建一个子类时:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  NSArray *touchesArray = [touches allObjects];
  UITouch *touch = (UITouch *)[touchesArray objectAtIndex:0];
  CGPoint point = [touch locationInView:self];
  self.position = point;}

函数touchesBegan 并不总是被调用。 如果我快速滑动,则永远不会调用 touchesBegan,而是直接调用 scrollViewDidScroll

我无法使用UIGesture (UITapGestureRecognizer, ... ),因为用户不会点击或滑动。

还有其他方法可以从 UIScrollView 获取位置吗?

编辑:

谢谢你:https://stackoverflow.com/a/15763450/1752843

UIScrollView 子类:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    self.position = point;
    return self;
}

【问题讨论】:

    标签: ios animation uiscrollview touch


    【解决方案1】:

    要检测滚动视图内的触摸位置,请尝试此代码

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
    [scrollView addGestureRecognizer:singleTap]; 
    
    - (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
     { 
       CGPoint touchPoint=[gesture locationInView:scrollView];
     }
    

    【讨论】:

    • 但是当我的滚动视图滚动时,函数“touchesBegan”或“touchesMoved”永远不会被调用。我想在使用滚动视图时获得触摸。
    • 即使“touchesBegan”为空,她也不会被调用。
    • 抱歉试试上面的代码。其他方法是你需要调用子类。欲了解更多详情,请查看此链接iosdevelopertips.com/user-interface/…
    • 要检测 touchesBegan 和 touchesMoved,不需要子分类。您可以将 UIScrollViewDelegate 添加到“h”文件和适用的函数以根据用户意图检测触摸。见stackoverflow.com/questions/993280/…
    【解决方案2】:

    您也可以使用scrollview.panGestureRecognizerlocationInView 方法在您的视图中获取触摸的坐标。这可以在 scrollView 的任何委托方法中,具体取决于您的要求。

    【讨论】:

      【解决方案3】:

      我在 SO 中看到了an answer,我认为它可以解决您的问题。似乎 SDK 不再将触摸处理方法传递给 UIScrollView 子类。因此,您应该执行hitTest: 将触摸传递给您的子类。

      查看答案了解更多信息。

      【讨论】:

      • 谢谢!我的代码:'- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{ self.position = point;回归自我; }'
      【解决方案4】:
      scrollView.delaysContentTouches = NO;
      

      这会让touchesBegan等触摸事件在scrollViewDidScroll之前触发。

      hitTest 是一种 hack,不应成为首选解决方案。

      【讨论】:

        猜你喜欢
        • 2013-06-25
        • 2016-02-06
        • 2013-04-12
        • 2011-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多