仅将触摸事件转发到正在触摸的视图 [英] Forwarding touch events only to the views being touched
问题描述
我有一个UIWindow有一个文本字段,一个按钮和一个表。
我想能够跟踪屏幕上的所有触摸,然后将它们转发到被触摸的元素。
I have a UIWindow with a text field, a button, and a table. I would like to be able to track all the touches on the screen and then forward them to the element being touched.
我已经阅读了关于覆盖<$在 sendEvent nofollow>苹果文档,但我还是不明白:
I have read about overriding sendEvent
in the Apple documentation but I still do not understand:
- 如何使用
hitTest
检索正在触摸的元素 - 如何转发触摸
- How to use
hitTest
to retrieve the element being touched - How to forward touches
<这是我到目前为止。
This is what I have so far.
- (void) sendEvent:(UIEvent *)event
{
for (UITouch *touch in [event allTouches])
{
/* Get coordinates of touch */
CGPoint point = [touch locationInView:self];
/* Get subview being touched */
/* something like this???
UIView *receiver = [self hitTest:point withEvent:event];
*/
/* Forward touch event to right view */
/* how??? */
}
[super sendEvent:(UIEvent *)event];
}
谢谢。
推荐答案
我不确定这是最好的解决方案,但我下面已经发布 here 。
I am not sure this is the best solution but I am following what has been posted here.
基本上我有一个UIView子类覆盖整个空间。这样的类包含对可以被触摸的所有元素的引用。
Basically I have a subclass of UIView covering the entire space. Such class contains a reference to ALL the elements that can be touched. (I wish there was a way to avoid that)
@interface SubclassUIView : UIView {
UITextField *text;
UITableView *table;
UIButton *button;
UIToolbar *toolbar;
}
这是实现:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
CGPoint tableHit = [table convertPoint:point fromView:self];
CGPoint buttonHit = [button convertPoint:point fromView:self];
CGPoint toolbarHit = [toolbar convertPoint:point fromView:self];
CGPoint messageHit = [text convertPoint:point fromView:self];
if ([table pointInside:tViewHit withEvent:event]) return table;
else if ([button pointInside:buttonHit withEvent:event]) return button;
else if ([toolbar pointInside:toolbarHit withEvent:event]) return toolbar;
else if ([text pointInside:messageHit withEvent:event]) return text;
return [super hitTest:point withEvent:event];
}
这篇关于仅将触摸事件转发到正在触摸的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!