需求的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. 1行代码为每个Controller自定义“TabBar”-b

    这篇文章大致会带你实现以下的功能,废话少说,先看东西: JPNavigationController.gif Q&A:Demo里都有那些东西? 01.关于自定义导航栏 01.第一个控制器的导航 ...

  2. 自定义tabBar

    ★★★★自定义tabBar★★★★★★★ Demo下载地址:https://github.com/marlonxlj/tabBarCustom.git 前言: 有的时候需求要对tabBar进行自定义的 ...

  3. IOS第二天-新浪微博 - 添加搜索框,弹出下拉菜单 ,代理的使用 ,HWTabBar.h(自定义TabBar)

    ********HWDiscoverViewController.m(发现) - (void)viewDidLoad { [super viewDidLoad]; // 创建搜索框对象 HWSearc ...

  4. iOS 隐藏自定义tabbar

    iOS  隐藏自定义tabbar -(void)viewWillAppear:(BOOL)animated { NSArray *array=self.tabBarController.view.su ...

  5. iOS开发之功能模块--关于自定义TabBar条

    只上项目中用到的代码: 1.实现重写TabBar的TabBarItem,然后在中间额外加一个按钮. #import <UIKit/UIKit.h> @interface BikeTabBa ...

  6. iOS开发项目之四 [ 调整自定义tabbar的位置与加号按钮的位置]

    自定义tabbar与按钮的添加 01 - 把系统的tabbar用我们自己的覆盖 LHQTabBar *lhqTabBar = [[LHQTabBar alloc]init]; [self setVal ...

  7. 关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究

      关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究 测试代码:http://git.oschina.net/Xiyue/TabBarItem_TEST 简 ...

  8. 第二篇、Swift_自定义 tabbar 的 badgeValue显示样式

    在实际的开发中,我们常常需要根据实际的需求,去改变bageValue的显示样式,默认是红色的背景,白色的字体颜色 使用方式: class BKTabBarController: UITabBarCon ...

  9. [iOS微博项目 - 1.6] - 自定义TabBar

    A.自定义TabBar 1.需求 控制TabBar内的item的文本颜色(普通状态.被选中状态要和图标一致).背景(普通状态.被选中状态均为透明) 重新设置TabBar内的item位置,为下一步在Ta ...

随机推荐

  1. 如何解决使用Gradle时出现的jar包冲突

    前言 在我之前使用Gradle的博文中已经提到,Gradle对依赖的管理是比较智能的,如果有两个包依赖于相同的包,而版本不同的时候,Gradle会进行自动的选择,从而避免jar包的冲突. 也就是说,在 ...

  2. docker 数据卷之进阶篇

    笔者在<Docker 基础 : 数据管理>一文中介绍了 docker 数据卷(volume) 的基本用法.随着使用的深入,笔者对 docker 数据卷的理解与认识也在不断的增强.本文将在前 ...

  3. 内置函数--global() 和 local()

    一 . globals :返回当前作用域内全局变量的字典.   >>> globals() {'__spec__': None, '__package__': None, '__bu ...

  4. flask项目开发中,遇到http 413错误

    在flask项目中,上传文件时后台报http 413 Request Entity Too Large 请求体太大错误! 解决的2种方法: 1.在flask配置中设置 MAX_CONTENT_LENG ...

  5. HighCharts之2D数值带有百分数的面积图

    HighCharts之2D数值带有百分数的面积图 1.HighCharts之2D数值带有百分数的面积图源码 AreaPercentage.html: <!DOCTYPE html> < ...

  6. eclipse怎么恢复原状

    eclipse怎么恢复原状 今天,我在写JSP页面时,将eclipse缩小窗口,后来我准备恢复,但是窗口却变成下面的状态

  7. VC++信息安全编程(13)Windows2000/xp/vista/7磁盘扇区读写技术

    有些时候,我们读取磁盘文件,会被hook.我们读到的可能并非实际的文件. 我们直接读取磁盘扇区获取数据. 实现磁盘数据的读写,不依赖WindowsAPI. [cpp] view plaincopy v ...

  8. Linux显示cat帮助信息并退出

    Linux显示cat帮助信息并退出 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ cat --help 用法:cat [选项]... [文件]... 将[文件 ...

  9. Java中的List转换成JSON报错(五)

    1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/beanu ...

  10. 芝麻HTTP:Scrapy-Splash的安装

    Scrapy-Splash是一个Scrapy中支持JavaScript渲染的工具,本节来介绍它的安装方式. Scrapy-Splash的安装分为两部分.一个是Splash服务的安装,具体是通过Dock ...