【问题标题】:UIScrollView touchesBeganUIScrollView touchesBegan
【发布时间】:2010-12-13 17:53:03
【问题描述】:

所以我想做的就是在用户触摸 UIScrollView 时播放声音。 UIScrollViewDelegate 具有该 scrollViewWillBeginDragging: 方法,但它仅在 touchMoved 上调用。我希望它在 touchBegan 上被调用。

试过 touchesBegan:withEvent: 但它没有收到任何触摸。 有人知道吗?

【问题讨论】:

    标签: iphone uiscrollview


    【解决方案1】:

    你需要继承滚动视图,然后实现touchesBegan方法。你还需要设置delayTouchDown属性为false。

    【讨论】:

      【解决方案2】:

      Rajneesh071 快速回答 4 将storyboard中scrollView的自定义类改为CustomScrollView

      import Foundation
      
      class CustomScrollView: UIScrollView {
          override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
              if !isDragging {
                  next?.touchesBegan(touches, with: event)
              } else {
              super.touchesBegan(touches, with: event)
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        改用点击手势识别器:

        UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch)];
        [recognizer setNumberOfTapsRequired:1];
        [recognizer setNumberOfTouchesRequired:1];
        [scrollView addGestureRecognizer:recognizer];  
        

        或者

        创建UIScrollView 的子类并实现所有

            -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        
        // If not dragging, send event to next responder
          if (!self.dragging){ 
            [self.nextResponder touchesBegan: touches withEvent:event]; 
          }
          else{
            [super touchesEnded: touches withEvent: event];
          }
        }
        -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        
        // If not dragging, send event to next responder
            if (!self.dragging){ 
             [self.nextResponder touchesBegan: touches withEvent:event]; 
           }
           else{
             [super touchesEnded: touches withEvent: event];
           }
        }
        -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
        
          // If not dragging, send event to next responder
           if (!self.dragging){ 
             [self.nextResponder touchesBegan: touches withEvent:event]; 
           }
           else{
             [super touchesEnded: touches withEvent: event];
           }
        }
        

        【讨论】:

        • @Rajneesh071 你在哪里处理布尔“拖动”?子类化UIScorllView时会自动处理吗?
        • @jeet.chanchawat 你的 UIScrollView 的子类,拖拽是 UIScrollView 的布尔属性
        【解决方案4】:

        改用点击手势识别器:

        UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch)];
        [recognizer setNumberOfTapsRequired:1];
        [recognizer setNumberOfTouchesRequired:1];
        [scrollView addGestureRecognizer:recognizer];
        

        【讨论】:

        【解决方案5】:

        关于问题here 的新更新(包括指向 ZoomScrollView 源代码的链接 + 关于 UIScrollView 内部的一些出色解释)。另外,请查看 Apple 更新的 ScrollViewSuite 示例。

        【讨论】:

          【解决方案6】:

          您还可以在控制器中响应这些事件。为此,您必须定义此功能(在您的控制器中):

          - (BOOL)canBecomeFirstResponder {
              return YES;
          }
          

          然后,在“viewDidAppear”中,您需要调用“becomeFirstResponder”:

          - (void)viewDidAppear:(BOOL)animated {
              [super viewDidAppear:animated];
              [self becomeFirstResponder];
          

          【讨论】:

            【解决方案7】:

            您应该在 UIScrollView 上添加 touchesBegan:withEvent:,而不是在 UIScrollViewDelegate 上。 UIScrollView 是 UIResponder 的子类,具有触摸事件。

            【讨论】:

            • 如果UIScrollView UIScrollViewDelegate呢?
            【解决方案8】:

            我认为你必须继承 UIScrollView 才能做到这一点。

            touchesBegan:withEvent: 只发送到 UIView 的子类。您可能正在控制器中实现 touchesBegan:withEvent: ,对吗?如果是这样,它不会从那里工作......

            或者,如果您将 UIView 的子类(您编写的)放在 UIScrollView 中,您也可以从那里捕获 touchesBegan 事件(但仅当用户触摸该特定子视图时)。 UIScrollView 默认将触摸传递给其子视图(请参阅 touchesShouldBegin:withEvent:inContentView: in UIScrollView)。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-01-15
              • 2020-10-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-03-20
              相关资源
              最近更新 更多