ios - UITableView 的 backgroundView 上的触摸事件

标签 ios iphone objective-c uitableview touch

我有一个 UITableViewControllertableView 有一个 backgroundView View (更重要的是,它是一个 MKMapView), 设置为

self.tableView.backgroundView = _mapView;

当前背景 View 显示在屏幕上(我也有一个透明的表头来实现这一点)。我无法使 mapView(tableViewbackgroundView)响应用户交互。我的表头位于 map View 上方,是 UIView 的子类,具有以下覆盖:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    LogDebug(@"Clic en transparent view");
    if (_transparent)
    {
        return nil;
    }
    return [super hitTest:point withEvent:event];
}

所以它应该通过事件;我不知道哪里出了问题,在其他类似问题中也没有找到任何解决方案。

必须将 mapView 保留为 tableView 的背景属性,因此请考虑到这一点。

有什么想法吗?

最佳答案

现在回答这个问题比较晚了。但是我在自己的项目中遇到了这个问题,经过一番研究我得到了答案。首先,我没有将后面的 View 分配给 tableView.backgroundView。相反,我使 tableView.backgroundColor 透明,然后将另一个 View 放在 TableView 下。

编辑: “在 table 下面”是指 tableViewanotherView 是同一父 View 的 subview ,并且 tableView.zPosition 大于 anotherView.zPosition.

然后我所做的就是覆盖 tableView:

(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event

并让 tableView 避免保存不在其单元格中的事件。

代码如下:

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {

    /// the height of the table itself.
    float height = self.bounds.size.height + self.contentOffset.y;
    /// the bounds of the table. 
    /// it's strange that the origin of the table view is actually the top-left of the table.
    CGRect tableBounds = CGRectMake(0, 0, self.bounds.size.width, height);
    return CGRectContainsPoint(tableBounds, point);

}

它对我来说就像一个魅力。希望它能帮助遇到此问题的任何其他人。

关于ios - UITableView 的 backgroundView 上的触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20444766/

相关文章:

java - 从 Objective C 到 java 的 SQLite 查询

ios - 隐藏 AVPlayerViewController 中的控件——仅在开始时

iphone - 如何旋转和调整 Sprite 像库中的照片大小

iOS 本地化 - 无法识别 es-US 与 es-MX

iOS 编辑按钮实现

ios - 将收件人显示为 UIButton

ios - 从嵌套完成 block 返回值

ios - 如何更改 userInterfaceLayoutDirection

c# - 无法在 iOS 上注册 FMOD 插件(使用 Unity3d)

objective-c - 使用 NSOperationQueue 或 GCD 进行异步解析