【问题标题】:why cant we detect a UITouch logic on UITableView, Objective C为什么我们不能在 UITableView、Objective C 上检测到 UITouch 逻辑
【发布时间】:2013-08-25 22:44:00
【问题描述】:

我搜索但不太明白为什么我们无法在 UITableView 上检测到 UITouch。我现在拥有的是:一个视图控制器,其视图中有一个表视图。请看下面的图片供您参考

在实现类中,我为每个 UITouch 方法启用断点

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

我注意到,当且仅当您触摸表格视图之外(橙色区域)时,才会调用这些断点

我不明白。我认为 UITableView 是 UIScrollView 的子类,它是 UIView 的子类,它是 UIResponder 的子类。这意味着应该调用 UITouch。 (如果我错了,请纠正我)

这里欢迎和赞赏所有的cmets。

【问题讨论】:

    标签: ios objective-c uitableview uitouch uiresponder


    【解决方案1】:

    与其篡改表格视图,不如使用手势识别器。您可以充当代理以确保所有交互同时进行,并根据需要启用和禁用手势。

    【讨论】:

      【解决方案2】:

      您可以通过将 UITableView 子类化为以下方式来检测触摸方法: 我测试它并成功打印“测试”

      //TestTable.h
      
      #import <UIKit/UIKit.h>
      
      @interface TestTable : UITableView
      
      @end
      
      //TestTable.m
      
      #import "TestTable.h"
      
      @implementation TestTable
      (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
      {
        NSLog("Test");
      }
      

      【讨论】:

        【解决方案3】:

        表格利用滚动视图来处理平移,它使用平移手势识别器。为什么不直接利用它呢?

        CGPoint location = [self.tableView.panGestureRecognizer locationInView:self.tableView];
        

        【讨论】:

          【解决方案4】:

          如果你想检测 UITableView 上的触摸,创建一个 tableview 的子类并添加实现 UIResponder 方法,canBecomeFirstResponder。

          @interface MyTableView: UITableView
          @end
          
          @implementation: MyTableView
            - (BOOL) canBecomeFirstResponder{
              return YES;
            }
          
            // then implement all other touch related methods, remember to call super in these methods
            // such that it correctly forwards the events to other responders
            - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
              // 
            }
          
            - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
          
            }
          
            - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
          
            }
          
            - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
          
            }
          @end
          

          【讨论】:

            猜你喜欢
            • 2012-07-25
            • 1970-01-01
            • 2021-02-15
            • 2012-05-04
            • 1970-01-01
            • 2023-03-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多