美文网首页专注iOS开发其他开发(iOS)
点击事件处理, 以及hitTest:withEvent:实现

点击事件处理, 以及hitTest:withEvent:实现

作者: rxdxxxx | 来源:发表于2016-04-03 18:41 被阅读6740次
  • 发送触摸事件后, 系统会将事件添加到系统UIApplication的事件管理队列中
  • UIApplication会在事件队列的最前端取出事件,然后分发下去,以便处理, 通常会把事件首先分发给KeyWindow处理
  • KeyWindow会在视图层次中找到一个最合适的视图来处理触摸事件,这也是处理事件过程的第一步.
  • 找到合适的视图后, 就会调用视图控件的相应方法
touchesBegan…
touchesMoved…
touchedEnded…
  • 如果父控件不能接受事件, 那么子控件就不能接受事件.

一个View是如何判断自己为最佳处理点击事件的View

// recursively calls -pointInside:withEvent:. point is in the receiver's coordinate system
- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event;


// default returns YES if point is in bounds
- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event;  

- hitTest: withEvent:方法的底层实现

此底层实现说明了, 一个view的子控件是如何判断是否接收点击事件的.

  • 此方法返回的View是本次点击事件需要的最佳View
// 因为所有的视图类都是继承BaseView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
//    NSLog(@"%@--hitTest",[self class]);
//    return [super hitTest:point withEvent:event];
    
    
    // 1.判断当前控件能否接收事件
    if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) return nil;
    
    // 2. 判断点在不在当前控件
    if ([self pointInside:point withEvent:event] == NO) return nil;
    
    // 3.从后往前遍历自己的子控件
    NSInteger count = self.subviews.count;
    
    for (NSInteger i = count - 1; i >= 0; i--) {
        UIView *childView = self.subviews[i];
        
        // 把当前控件上的坐标系转换成子控件上的坐标系
     CGPoint childP = [self convertPoint:point toView:childView];
        
       UIView *fitView = [childView hitTest:childP withEvent:event];
        
        
        if (fitView) { // 寻找到最合适的view
            return fitView;
        }
        
        
    }
    
    // 循环结束,表示没有比自己更合适的view
    return self;
    
}

根据以上的实现, 那么我们就可以重写这个方法, 做一些非常规的效果

  • 此方法返回的View是本次点击事件需要的最佳View
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    
    // 当前控件上的点转换到chatView上
    CGPoint chatP = [self convertPoint:point toView:self.chatView];
    
    // 判断下点在不在chatView上
    if ([self.chatView pointInside:chatP withEvent:event]) {
        return self.chatView;
    }else{
        return [super hitTest:point withEvent:event];
    }
    
}

响应者事件/链条

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
  • began方法的默认行为是将事件顺着响应者链条向上传递, 将事件交给上一个响应者处理.

现在有两个View, 一个Blue ,另一个是Yellow.

Yellow是Blue的子控件.

此时你点击Yellow,就会触发Yellow的touchesBegan方法

如何你没有重写Yellow的touchesBegan方法或者调用了[super touchesBegan...],

那么它会触发他下一个响应者的touchesBegan方法, 也就是Blue的touchesBegan方法.

如果这个View是Controller的View, 那么这个View的下一个响应者是控制器.

如果控制器也不处理, 那么把这个事件传递给Controller所在Window.

如果Window也不处理, 那么把事件给UIApplication

如果事件到最后都没人处理, 那么这个事件会被UIApplication丢弃.

相关文章

网友评论

  • Corbin___:hitTest: withEvent:方法的底层实现是靠猜想的把,apple源码看不到的
    rxdxxxx:是猜的
  • f170d29955a5:老铁,我自定义了一个类似于咸鱼tabbar的样式,就是tabbar有五个按钮,中间的按钮高出tabbar一部分,为了解决高出的那一部分不能点击的问题,我重写了- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event方法,在通过导航栏push进去的页面没有导航栏了,但是高出的那一部分还是能点击,兄弟有没有方法在导航栏push进去的页面关了那一部分的点击?
    f170d29955a5:谢谢了
    f170d29955a5:@RedRain 在首页上,tabbar高出的那一块能点击,push到一个页面,并且隐藏了tabbar,但是tabbar高出的那一块虽然隐藏了但还是能点击,我有空研究一下你给的这个
    rxdxxxx:没看懂你最后几句什么意思, 但是如果你想扩大按钮的点击区域,可以下这个 `https://github.com/stevendinggang/UIButton-HitRect`
  • 滚滚猫:我在UIViewController上的self.view添加一个button,button已经超出view的范围了,因为我设置了self.view的偏移量,所以button是可视的,但不能接受点击事件。
    这个方法 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 是需要写到被点击按钮的父view里吧,这样UIViewController里的self.view是没有基类的吧,要怎么做呀
    rxdxxxx:@__weak 应该是可以的,你试试在方法里返回你要处理的view
    __weak:我想问问我上面覆盖了一个和屏幕大的View,我这个View可以点击的。另外我下面还有个View也是要点击,想问问用这个- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event方法能实现不,如果能是怎么实现的啊?菜鸟求救
    rxdxxxx:@滚滚猫 重写控制器的loadView

本文标题:点击事件处理, 以及hitTest:withEvent:实现

本文链接:https://www.haomeiwen.com/subject/tzrulttx.html