zoukankan      html  css  js  c++  java
  • 自定义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的位置。

  • 相关阅读:
    2016年回家的大概经过
    [转载][记录]shell 批量修改文件名
    Tinymce4 中Ajax多次加载时,会出现菜单在第二次进入时,显示的下拉菜单在左上角
    PHP生成HTML页面顶部出现空白部分(&#65279字符?)
    tcpdf MultiCell line break
    [转载][记录]javascript生成不重复的随机数
    [转载]PHP 字符串替换中文
    PHP 使用get_class_methods()和array_diff() 兩個相同的類中方法差集
    mysql datetime 排序
    highcharts 去掉右下角链接
  • 原文地址:https://www.cnblogs.com/xjshi/p/6417994.html
Copyright © 2011-2022 走看看