自定义TabBar之理解hittest
需求的TabBar是这样的:5个 tabItem, 中间的那个 item 部分超出系统默认TabBar的上边界。
那么实现的关键点就是如何在点击它突出的部分的时候,也可以正常获得响应。我来把问题简化,我把下图中的红色的视图(类型为RedView,继承自UIView)称为 redview,蓝色的视图(类型为BlueView,继承自UIView)称为 blueview。redview 添加在试图控制器的视图上,blueview 添加在 redview上。

在 RedView 和 BlueView 的- (UIView *)hitTest:withEvent:方法和- (void)touchesBegan:withEvent:都保持默认实现的情况下,点击 blueview 超出 redview 的部分,两个视图的- (void)touchesBegan:withEvent:均不执行。
下面我来改造一下 RedView 的- (UIView *)hitTest:withEvent:方法,如下:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
UIView *blueview = self.subviews.firstObject;
if (CGRectContainsPoint(blueview.frame, point)) {
return blueview;
}
return view;
}
这样重写以后,再点击上边说的那部分, RedView 的 hittesst 就会把 blueview 作为 hittest view,因此会执行到 blueview 的- (void)touchesBegan:withEvent:方法。
为什么会这样?
我们可以看一下- (UIView *)hitTest:withEvent:的说明(读原文吧,比我描述的清楚):
This method traverses the view hierarchy by calling the pointInside:withEvent: method of each subview to determine which subview should receive a touch event. If pointInside:withEvent: returns YES, then the subview’s hierarchy is similarly traversed until the frontmost view containing the specified point is found. If a view does not contain the point, its branch of the view hierarchy is ignored. You rarely need to call this method yourself, but you might override it to hide touch events from subviews.
This method ignores view objects that are hidden, that have disabled user interactions, or have an alpha level less than 0.01. This method does not take the view’s content into account when determining a hit. Thus, a view can still be returned even if the specified point is in a transparent portion of that view’s content.
Points that lie outside the receiver’s bounds are never reported as hits, even if they actually lie within one of the receiver’s subviews. This can occur if the current view’s clipsToBounds property is set to NO and the affected subview extends beyond the view’s bounds.
想要跟深入的了解这部分内容,可以阅读 Response Chain 相关的文档。这里贴上地址:https://developer.apple.com/library/content/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/event_delivery_responder_chain/event_delivery_responder_chain.html
到这里如何定义需求中的Tabbar怎么实现就很明显了。直接放个Button在中间就好了,然后调整其他四个TabItem的位置。
自定义TabBar之理解hittest的更多相关文章
- 1行代码为每个Controller自定义“TabBar”-b
这篇文章大致会带你实现以下的功能,废话少说,先看东西: JPNavigationController.gif Q&A:Demo里都有那些东西? 01.关于自定义导航栏 01.第一个控制器的导航 ...
- 自定义tabBar
★★★★自定义tabBar★★★★★★★ Demo下载地址:https://github.com/marlonxlj/tabBarCustom.git 前言: 有的时候需求要对tabBar进行自定义的 ...
- IOS第二天-新浪微博 - 添加搜索框,弹出下拉菜单 ,代理的使用 ,HWTabBar.h(自定义TabBar)
********HWDiscoverViewController.m(发现) - (void)viewDidLoad { [super viewDidLoad]; // 创建搜索框对象 HWSearc ...
- iOS 隐藏自定义tabbar
iOS 隐藏自定义tabbar -(void)viewWillAppear:(BOOL)animated { NSArray *array=self.tabBarController.view.su ...
- iOS开发之功能模块--关于自定义TabBar条
只上项目中用到的代码: 1.实现重写TabBar的TabBarItem,然后在中间额外加一个按钮. #import <UIKit/UIKit.h> @interface BikeTabBa ...
- iOS开发项目之四 [ 调整自定义tabbar的位置与加号按钮的位置]
自定义tabbar与按钮的添加 01 - 把系统的tabbar用我们自己的覆盖 LHQTabBar *lhqTabBar = [[LHQTabBar alloc]init]; [self setVal ...
- 关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究
关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究 测试代码:http://git.oschina.net/Xiyue/TabBarItem_TEST 简 ...
- 第二篇、Swift_自定义 tabbar 的 badgeValue显示样式
在实际的开发中,我们常常需要根据实际的需求,去改变bageValue的显示样式,默认是红色的背景,白色的字体颜色 使用方式: class BKTabBarController: UITabBarCon ...
- [iOS微博项目 - 1.6] - 自定义TabBar
A.自定义TabBar 1.需求 控制TabBar内的item的文本颜色(普通状态.被选中状态要和图标一致).背景(普通状态.被选中状态均为透明) 重新设置TabBar内的item位置,为下一步在Ta ...
随机推荐
- caffe之路-SIGTERM信号捕捉
Caffe在1.0版本仅支持两种信号的处理: 1) SIGHUP 2) SIGINT SIGHUP:caffe接收到此信号后进行snapshot,并不会中断caffe的训练. SIGINT:caffe ...
- cronatab周期性任务
要执行周期性任务,要保证服务运行.服务名为crond:service crond start ; systemctl start crond; 配置文件 /etc/crontab cron的日志文件 ...
- bash文件的详细解读
一.bash的分类 1. 按生效范围分类 全局生效 /etc/profile /etc/profile.d/*.sh /etc/bashrc 个人用户生效 ~/.bash_profile ~/.bas ...
- 修改android 开机画面
对于使用安卓手机的人来说,能够自由定制手机的各种界面是每个用户之所以喜欢安卓系统的最根本的缘由,比如手机的开机界面中的bootanimation.zip文件.本文就如何修改开机界面,做一个简单的流程介 ...
- ssh禁止密码登录
1.root用户登陆后,运行以下第一句指令,其他根据提示进行输入: ssh-keygen -t rsaGenerating public/private rsa key pair.Enter file ...
- dojo页面调试出错
今天,我在调试dojo页面时,出现一些未定义的错误,这些错误只显示在引入的js的那一行,并没有指出是页面哪儿出问题了. 出现的问题一: neteaseTracker is not defined 出现 ...
- l【linux】linux rpm包命名规范
RPM包的一般格式为:name-version-arch.rpmname-version-arch.src.rpm name:软件包名称.version:带有主.次和修订的软件包版本.arch:硬件平 ...
- HTML5之contenteditable属性
HTML5之contenteditable属性 1.功能说明 (1)功能:允许用户编辑元素中的内容 (2)说明:是一个布尔值,false是不能编辑,true为可编辑 2.分析实例 (1)content ...
- IOS开发之XCode学习007:UIWindow对象
此文学习来源为:http://study.163.com/course/introduction/1002858003.htm #import "AppDelegate.h" @i ...
- CSS3动画以及animation事件
1.CSS3动画以及animation事件的定义 animation :name duration timing-function delay iteration-count direction an ...