繁体   English   中英

触摸UITableView的backgroundView上的事件

[英]Touch events on UITableView's backgroundView

我有一个UITableViewController ,并且tableView有一个视图作为backgroundView (更多,它是一个MKMapView ),设置为

self.tableView.backgroundView = _mapView;

目前,背景视图显示在屏幕上(我还有一个透明的表头来实现这一点)。 我无法使mapViewtableViewbackgroundView )响应用户交互。 我的表头,在地图视图上方,是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的background属性,所以请考虑到这一点。

任何想法?

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

编辑:通过“在表下”我的意思是tableViewanotherView View是相同父视图的子视图, 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);

}

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

我想touchEvents不能传递给tableView的backgroundView。 所以,我做的是,

对于作为UIVIEW的子类的headerView,我添加了一个类属性作为mapView。 HeaderView.h中

@property (nonatomic,assign) UIView *mapView;

并且,在tableViewController中

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HeaderView *view =[[TestView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
    view.mapView =tableView.backgroundView;
    return view;
}

并在HeaderView中覆盖hitTest并返回此mapView。

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

现在mapView接收到touchEvent并相应地做出响应。
。添加您可能感兴趣的额外代码。 希望这可以帮助。

Santhu的回答几乎对我有用,我把测试通过mapView而不是只返回它,我也需要进行点检查,因为我只想通过标题触摸事件,所以在HeaderView上有我的版本的命中测试:

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    if(point.y > self.frame.size.height){
        return nil;
    }
    return [self.mapView hitTest:point withEvent:event];
}

把它放在一个UITableView子类中,缺点是你不能再点击单元格,但希望它是一个指向正确方向的指针。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}

仅供参考,这是一个旧帖子,UITableView.backgroundView现在接收触摸。 因此,如果您的backgroundView中的内容没有接收到触摸,则由于其他一些问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM