参考:
[1]http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MultitouchEvents/MultitouchEvents.html#//apple_ref/doc/uid/TP40009541-CH3-SW20
情景:
如果你有一个全屏、透明的UIView盖在目前界面的上面,这个uivew不做显示用,只是收集用户在屏幕上的move 时的点。但要允许用户在点击和长按时,全屏view背后的view做出反应,这时全屏的view相当于不存在。
解决方案:
方案一
设置该全屏、透明view的 userInteractionEnabled 为NO,截获UIApplication的SendEvent ,在其中记录move时的所有点。这时,用户所有的touch事件都按原来执行,这个全屏的view真像不存在一样。
利用objective-c runtime相关代码核心实现如下:
//Swap the implementations of our interceptor and the original sendEvent: Method oldMethod = class_getInstanceMethod(self, @selector(sendEvent:)); Method newMethod = class_getInstanceMethod(self, @selector(interceptSendEvent:)); method_exchangeImplementations(oldMethod, newMethod);
方案二
设置该全屏、透明view的 userInteractionEnabled 为YES,重写父类的四个事件处理方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)forwardTouchBegan:(id)obj { NSArray* array = (NSArray*)obj; NSSet* touches = [array objectAtIndex:0]; UIEvent* event = [array objectAtIndex:1]; [[self nextResponder] touchesBegan:touches withEvent:event]; }
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { touchMove_ = NO; [self performSelector:@selector(forwardTouchBegan:) withObject:[NSArray arrayWithObjects:touches,event,nil] afterDelay:0.5]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { touchMove_ = YES; [[self class] cancelPreviousPerformRequestsWithTarget:self]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if (!touchMove_) { [[self nextResponder] touchesEnded:touches withEvent:event]; } }
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget;
不过方案二 跟 背景view与全屏viewview层次相关,如果两则不同的window 则 通过[self nextResponder] 背景view可能还是接收不到事件。