2015-01-13 55 views
2

问:有没有办法从上海华接收触摸直接在它的子视图(即触动子视图的边界之外)?iOS设备上接收上海华的触摸事件,在它的子视图

我想避免授权(正式/非正式),NSNotification,代理或任何其他中介解决方案将触摸事件从一个视图转发到另一个视图。

+2

没有其他办法。阅读有关UIResponderChain的理论。 – Tirth

+0

我读过它,它并不直接说它不可能。它甚至表明这是返回pointInside的BOOL值的问题:withEvent :, quote:“如果传入hitTest:withEvent:的点不在视图的边界内,则首先调用pointInside:withEvent:方法返回NO,该点被忽略,并且hitTest:withEvent:返回nil。“ –

回答

1

这将做到这一点。在子视图中覆盖pointInside。希望能够满足您的要求。

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 
{ 
    point = [self convertPoint:point toCoordinateSpace:self.superview]; 
    CGRect frame = self.superview.frame; 
    if (CGRectContainsPoint(frame, point)) { 
     return YES; 
    } 
    return [super pointInside:point withEvent:event]; 
} 
+0

非常感谢!使用此方法分类的SKView完美地工作。 –

+0

不幸的是,我发现了一个问题:有时不符合条件,但无条件返回YES修复它​​(?)。有什么想法吗? 我的UIView树是:UIView A(root)> UIView B> UIView C> UIView D在同一节点上的SKView。只能从本身和UIView C接收SKView。 –

+0

@KrzysztofPrzygoda - 我认为这是您的视图层次结构中的一些问题,但我无法从您的评论中充分说出。我建议你尝试记录点和帧,找出导致它失败的原因。另请参阅[事件传递]的苹果指南(https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/event_delivery_responder_chain/event_delivery_responder_chain.html)以及[在smnh上写的这篇漂亮的文章。我](http://smnh.me/hit-testing-in-ios/) – foundry

相关问题