子视图中的手势识别器出现问题
我遇到了一个相当基本的问题,我环顾四周(在这里,谷歌等),但没有找到解决方案:
I'm having a rather basic problem, I've looked around (here, google, etc) and haven't found a solution for this:
在我的视图控制器的 viewDidLoad
中,我有这个:
In my View Controller's viewDidLoad
, I have this:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(myfunc:)];
//I have a UIScrollView named "containerView"
//here's some code that creates an UIView in a variable named "myView"
//this works fine, I can see "myView" when I run it
[containerView addSubview:myView];
[myView addGestureRecognizer:longPress];
然后我在同一个类中有这个函数:
and then I have this function in the same class:
- (void)myfunc:(UIRotationGestureRecognizer *)recognizer
{
NSLog(@"hola!"); //never runs
}
对 NSLog
的调用永远不会运行.我做错了什么?
The call to NSLog
never runs. What am I doing wrong?
编辑
一些额外的信息:似乎没有任何触摸事件发送到子视图.但是,我尝试添加一个带有按钮的 UIView,全部在 UIScrollView 中,并且按钮接收触摸事件就好了,所以问题仅在于以编程方式添加的子视图.
Some extra info: it seems no touch events are ever sent to the subview. However, I tried adding an UIView with a button inside, all in the UIScrollView, and the button receives the touch event just fine, so the problem is only with programmatically added subviews.
奇怪的是,在 UIScrollView 内部添加了一个容器" UIView,然后在这个容器内添加其他子视图,使它工作.现在触摸事件被发送到子视图.
Strangely enough, adding a "container" UIView inside the UIScrollView, and then the other subviews inside this container, made it work. Now touch events are sent to the subviews.