在tableview之外的触摸上隐藏UITableView [英] Hide UITableView on touches outside the tableview

查看:67
本文介绍了在tableview之外的触摸上隐藏UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的 UITableView ,该视图在加载视图时被隐藏.当我单击" SHOW " UIButton 时,myTableView.hidden=NO;

I have a small UITableView that is hidden when the view is loaded. When i click on "SHOW" UIButton, the UITableView is made visible by myTableView.hidden=NO;

我想在用户触摸其框架外时隐藏 UITableView .谢谢你的帮助!

I want to hide UITableView when a user touches outside its frame. Thanks for any help!

推荐答案

最佳方法

简单.在显示UITable视图之前,再添加一个灰色/透明视图,然后在其上添加轻击手势识别器以将其隐藏.就是这样.

Simple.Before show up the UITable View add one more grayed out/Transparent view then add tap gesture recogniser on it to hide it . That's it.

  1. 先显示叠加层视图-alpha将为0.5f,背景颜色应为纯色.

  1. Show Overlay View first - alpha will be 0.5f and background color should be clear color.

显示表格视图.

注意:叠加视图应具有点击识别器,它将隐藏覆盖图和表格视图

NOTE: over lay view should have tap recogniser which will hide the overlay and table view

已加载

UITapGestureRecognizer *tapRecog = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(overlayDidTap:)];

[myOverLayView addGestureRecognizer:tapRecog];



- (void)overlayDidTap:(UITapGestureRecognizer *)gesture
{

    //hide both overlay and table view here

}

错误的方法

我们不应该在主视图本身上添加拍子识别器.因为它可能有很多 里面的控件.因此,当用户点击它时.它将执行其操作.所以要避免 我们可以通过上述方法模拟相同的行为

We should not add tap recogniser on main view itself. Because it may have lots of controls inside of it. so when user tap on it. it will perform its operation. So to avoid it we can simulate the same behaviour by above approach

这篇关于在tableview之外的触摸上隐藏UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆