自定义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 ...
随机推荐
- hdu1995 汉诺塔V
可以直接把前K-1个罗盘全部忽略了,因为移动前K-1个罗盘不会影响第K个. 也就是相当于只移动剩下的n-k-1个罗盘,当只移动第k个罗盘时,f(k)=1;当要哟东第k个和第k+1个时,就必须先把第k个 ...
- Python接口测试,Requests模块讲解:GET、POST、Cookies、Session等
文章最下方有对应课程的视频链接哦^_^ 一.安装.GET,公共方法 二.POST 三.Cookies 四.Session 五.认证 六.超时配置.代理.事件钩子 七.错误异常
- Quartz基本使用
1.Quartz概述:简单的说就是java的一个定时任务的框架,没有那么复杂的解释,跟前端的定时器一样.在了解它之前,首先熟悉几个概念. 2.基本概念 2.1 Job:表示一个工作,要执行的具体内容. ...
- com.alibaba.druid.sql.parser.ParserException: syntax error, QUES %, pos 80 like报错解决
最近,把各应用的jdbc连接池统一从dbcp2改成了druid,运行时druid报sql解析错误,如下: select * from test where 1=1 &l ...
- AndFix
AndFix 是阿里巴巴开源的 Android 应用热修复工具,帮助 Anroid 开发者修复应用的线上问题.Andfix 是 "Android hot-fix" 的缩写.支持 A ...
- My97DatePicker日历控制按日、按周和按月选择
My97DatePicker日历控制按日.按周和按月选择 1.设计源码 <%@ page language="java" import="java.util.*&q ...
- 芝麻HTTP:Python爬虫入门之Cookie的使用
为什么要使用Cookie呢? Cookie,指某些网站为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据(通常经过加密) 比如说有些网站需要登录后才能访问某个页面,在登录之前,你想抓 ...
- Java web切面编程
在我们的 web开发中 我们在 对公用的 一些方法 我们需要抽取出来 这样达到 代码的冗余 今天 我利用项目上用的AOP的 实际 应用做了一个整理 首先 xml配置 扫描 <?xm ...
- openfec的学习笔记
openfec实现了多种纠删码的算法实现,就包括Reed-Solomon算法.其基本使用流程为:输入n个原始包的分组后,计算生成k个额外的冗余包,后续将这n+k包送到接收端,若发生原始包丢包,但只要总 ...
- babel-runtime和babel-polyfill两者区别优缺点
先说两种方式的原理: babel-polyfill 使用场景 Babel 默认只转换新的 JavaScript 语法,而不转换新的 API.例如,Iterator.Generator.Set.Maps ...