【问题标题】:UIScrollView overlays UIView but enables its touch events?UIScrollView 覆盖 UIView 但启用其触摸事件?
【发布时间】:2013-03-13 08:50:50
【问题描述】:

是否可以同时处理 UIScrollView 的滚动事件和 UIView 后面的触摸事件? 我发现 UIView 只在 UIScrollView 禁用自己的用户交互时处理它的触摸事件。

【问题讨论】:

    标签: uiview uiscrollview scroll touch overlay


    【解决方案1】:

    首先在您的视图中添加滚动视图和 in.h 文件写入委托<UIGestureRecognizerDelegate,UIScrollViewDelegate>

    在你看来DidLoad 写

        UITapGestureRecognizer *singleFingerTap =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(handleSingleTap:)];
        [self.view addGestureRecognizer:singleFingerTap];
        [singleFingerTap release];
    
        scrl.scrollEnabled = YES; // scrl is my UIScrollView
        scrl.delegate = self;
        scrl.contentSize=CGSizeMake(320, 680);
        [self.view addSubview:scrl];
    

    并在 handleSingleTap 方法中编写您的代码。它也会滚动,如果您点击视图,则会调用 handleSingleTap 方法。

    【讨论】:

      【解决方案2】:

      在让 UIScrollView 覆盖 SpriteKit 视图时,我遇到了类似的问题,以便用户可以滚动游戏内的大菜单。滚动视图“窃取”了所有点击,我无法按下滚动视图后面的按钮,例如我的“后退按钮”。我在滚动视图中也有按钮,在我原来的 sprite kit 场景中我有处理程序 - 我讨厌移动到我的可滚动视图中。

      因此,我通过简单地将所有触摸事件发送到超类和超视图来解决此问题。 我不确定是否还有什么大的缺点,但我现在可以触摸和滚动。

      -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
      {
          [self.superview touchesBegan:touches withEvent:event];
          [super touchesBegan:touches withEvent:event];
      }
      
      -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
      {
          [self.superview touchesMoved:touches withEvent:event];
          [super touchesMoved:touches withEvent:event];
      }
      
      -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
      {
          [self.superview touchesCancelled:touches withEvent:event];
          [super touchesCancelled:touches withEvent:event];
      }
      
      -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
      {
          [self.superview touchesEnded:touches withEvent:event];
          [super touchesEnded:touches withEvent:event];
      }
      

      【讨论】:

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