hitTest事件


iOS中,不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件.处理事件的对象叫@“响应者对象”

事件可以分为3大类型. 1.触摸事件 2.加速计事件 3.远程控制事件

// 触摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesCancelled:(nullable NSSet *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesEstimatedPropertiesUpdated:(NSSet * _Nonnull)touches NS_AVAILABLE_IOS(9_1);

// 加速计事件
- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
- (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);

// 远程控制事件
- (void)remoteControlReceivedWithEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(4_0);

在这里主要说下触摸事件.
一.触摸事件
UITouch:
UITouch的一些属性和方法:

// 触摸产生时所处的窗口
@property(nullable,nonatomic,readonly,strong) UIWindow                        *window;
// 触摸产生时所处的视图
@property(nullable,nonatomic,readonly,strong) UIView                          *view;
// 短时间内点按屏幕的次数(可判断单击 双击)
@property(nonatomic,readonly) NSUInteger          tapCount;
// 当前触摸事件所处的状态(是按下,还是移动等等..  是个枚举)
@property(nonatomic,readonly) UITouchPhase        phase;

typedef NS_ENUM(NSInteger, UITouchPhase) {
    UITouchPhaseBegan,             // whenever a finger touches the surface.
    UITouchPhaseMoved,             // whenever a finger moves on the surface.
    UITouchPhaseStationary,        // whenever a finger is touching the surface but hasn't moved since the previous event.
    UITouchPhaseEnded,             // whenever a finger leaves the surface.
    UITouchPhaseCancelled,         // whenever a touch doesn't end but we need to stop tracking (e.g. putting device to face)
};

// 触摸的位置在参数view中得位置 (拖拽时可用到)
- (CGPoint)locationInView:(nullable UIView *)view;

二.事件的产生和传递
发生触摸事件后,系统会将该事件加入到一个由UIApplication管理的事件队列中.
UIApplication会取出队列最前面的事件,并将分发下去.通常先发送给应用程序的主窗口(keyWindow).
主窗口会在视图层次结构中找打一个最合适的视图来处理触摸事件.这只是整个事件处理过程的第一步.
找到合适的控件后,就会调用视图控件的touchBegan,touchMoved,touchEnded等..方法.

比如:

hitTest事件_第1张图片
hitTestView.png


点击了绿色的view
UIApplication->UIWindow->White->Green
点击了蓝色的view
UIApplication->UIWindow->White->Orange->Blue
点击了黄色的view
UIApplication->UIWindow->White->Orange->Blue->Yellow

注意!!
如果父控件不能接收触摸事件,那么子控件就不可能接收到触摸事件.

不接收触摸事件的3种情况
1.没打开用户交互

@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;  // default is YES. if set to NO, user events (touch, keys) are ignored and removed from the event queue.

2.隐藏
hidden = YES
3.透明
alpha < 0.01

UIImageView的userInteractionEnabled默认是NO,所以UIImageView对象的子控件默认是接收不到触摸事件的.

找到最合适的控件来处理事件的主要方法:

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

三.原理:

1.先判断自己是否能接收事件.
2.触摸点是否在自己身上.
3.从后往前遍历子控件,重复前面1和2 两步.
为什么从后往前遍历子控件是因为view的subview中 后加的在上面,为了减少性能的消耗.
4.如果没有符合条件的子控件.那么就自己最适合处理.(递归的出口)

// 找最合适的view
// point是白色View的坐标系上的点
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    // 1.判断自己能否接收事件
    if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) return nil;

    // 2.判断点在不在当前控件上面
    if (![self pointInside:point withEvent:event]) return nil;

    // 3.去找有没有比自己更合适的view
    // 从后往前遍历自己的子控件
    int count = self.subviews.count;

    for (int i = count - 1; i >= 0; i--) {
        // 获取子控件
        UIView *childView = self.subviews[i];

        // 转换坐标系
        // 把自己坐标系上的点转换成子控件做坐标系上的点
        CGPoint childPoint = [self convertPoint:point toView:childView];

        UIView *fitView = [childView hitTest:childPoint withEvent:event];
        // 找到最合适的view
        if (fitView) {
            return fitView;
        }

    }

    // 没有找到比自己更合适的view
    return self;
}

解释一下:
比如点击的是上面各种颜色那个图的 第3层的蓝色.
事件传递顺序:
UIApplication -> 传给UIWindow
UIWindow -> 传给白色View
白色view-> 自己可以接收事件,点在自己身上.从后往前遍历子控件 得到橙色view
橙色view-> 自己可以接收事件,点在自己身上.从后往前遍历子控件 得到红色view(自己可以接收事件,点不在自己身上. 结束) 继续遍历得到蓝色view
蓝色view-> 自己可以接收事件,点在自己身上.从后往前遍历子控件 得到黄色view
黄色view-> 自己可以接收事件,点不在自己身上. 结束.

所以蓝色view才是处理事件最合适的view.

这也是hitTest的实现原理吧.


关联补充:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event

{

}


   这个函数的用处是判断当前的点击或者触摸事件的点是否在当前的view中。

   它被hitTest:withEvent:调用,通过对每个子视图调用pointInside:withEvent:决定最终哪个视图来响应此事件。如果 PointInside:withEvent:返回YES,然后子视图的继承树就会被遍历(遍历顺序中最先响应的为:与用户最接近的那个视图。 it starts from the top-level subview),即子视图的子视图继续调用递归这个函数,直到找到可以响应的子视图(这个子视图的hitTest:withEvent:会返回self,而不是nil);否则,视图的继承树就会被忽略。


    当我们需要重写某个UIView的继承类UIViewInherit的时候,如果需要重写hitTest:withEvent:方法,就会出现是否调用[super hitTest:withEvent:]方法的疑问?究竟是否需要都是看具体需求,这里只是说明调与不调的效果。

    如果不调用,那么重写的方法hitTest:withEvent:只会调用重写后的代码,根据所重写的代码返回self或nil,如果返回self那么你的这个UIViewInherit类会接受你的按键,然后调用touches系列方法;否则返回nil那么传递给UIViewInherit类的按键到此为止,它不接受它的父view给它的按键,即不会调用touches系列方法。这时,PointInside:withEvent:几乎没有作用。

    如果调用,那么[super hitTest:withEvent:]方法首先是根据PointInside:withEvent:的返回值决定是否递归调用所有子View的hitTest:withEvent:方法。对于子View的hitTest:withEvent:方法调用也是一样的过程,这样一直递归下去,直到最先找到的某个递归层次上的子View的hitTest:withEvent:方法返回非nil,这时候,调用即结束,最终会调用这个子View的touches系列方法。

 

     如果我们不想让某个视图响应事件,只需要重载 PointInside:withEvent:方法,让此方法返回NO就行了。不过从这里,还是不能了解到hitTest:WithEvent的方法的用途。


http://blog.sina.com.cn/s/blog_87bed3110100t5cf.html

http://blog.csdn.net/iefreer/article/details/4754482


hitTest:withEvent:调用过程


The implementation of hitTest:withEvent: in UIResponder does the following:

  • It calls pointInside:withEvent: of self
  • If the return is NO, hitTest:withEvent: returns nil. the end of the story.
  • If the return is YES, it sends hitTest:withEvent: messages to its subviews. it starts from the top-level subview, and continues to other views until a subview returns a non-nil object, or all subviews receive the message.
  • If a subview returns a non-nil object in the first time, the first hitTest:withEvent: returns that object. the end of the story.
  • If no subview returns a non-nil object, the first hitTest:withEvent: returns self

This process repeats recursively, so normally the leaf view of the view hierarchy is returned eventually.

However, you might override hitTest:withEvent to do something differently. In many cases, overriding pointInside:withEvent: is simpler and still provides enough options to tweak event handling in your application.

你可能感兴趣的:(iOS_OC)