iPhone SDK - 将触摸事件从UIViewController转发到子视图

3
我有一个UIViewController,其中包含名为customSubView1的UIView子类。 然后在customSubView1上,我有另一个名为customSubView2的UIView子类。
当我将touchesBegan / touchesMoved等放入UIViewController类中时,可以捕获所有子视图上的触摸事件。 但是我想能够在我的自定义类中处理它们。
我一直在阅读UIViewController类需要“转发”触摸事件到子视图,但我还没有找到任何示例代码来实现这一点。 有人有任何想法吗?
非常感谢, Brett
2个回答

0

尝试为您的UIView对象设置userInteractionEnabled属性:

myUIView.userInteractionEnabled = YES;

0

在你的视图中实现touches...方法。


它们都在,但没有一个被调用。 - Brett
你的视图是否存在,可见,并且 userInteractionEnabled = YES(对于直接的 UIView 子类应该是默认值)?它们可能在 scrollView 中吗?你是否尝试过最基本的方法,以排除其他错误的原因?CustomViewClass *myCustomTestView = [[CustomViewClass alloc] initWithFrame:CGRectMake(100,100,100,100)]; myCustomView.userInteractionEnabled = YES; myCustomView.backgroundColor = [UIColor redColor]; [self.view addSubview:myCustomView]; 应该可以工作。 - fzwo
是的,那只是为了万无一失。有时候最明显的问题被忽略了。他们在scrollView里吗?scrollView可能会“抢走”触摸事件。您尝试过我的非常基本的示例吗? - fzwo
我遇到了完全相同的问题,我尝试了上面的代码CustomViewClass *myCustomTestView = [[CustomViewClass alloc] initWithFrame:CGRectMake(100,100,100,100)]; myCustomView.userInteractionEnabled = YES; myCustomView.backgroundColor = [UIColor redColor]; [self.view addSubview:myCustomView]; 并在UIView子类上调用touchesBegan。这个问题有解决方案吗? - CodeOwl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接