8

我有一个 UIGestureRecognizer,我想在两个不同的 UIView 上工作,它们都在 UiViewController 的同一视图层次结构中。UIGestureRecognizer 的动作在每一个上都差不多,所以我希望调用相同的函数(很明显),我会在运行时告诉我正在处理哪些 UIView。但是怎么做?我看不到 UIGestureRecognizer 正在携带对象信息。我是否错过了文档中的这一行,或者gestureRecognizer不知道它附加到哪个对象上被调用?似乎语言的重点在于它会知道。

或者,也许我误解了课程的意图,我不应该:

UITapGestureRecognizer *dblTap = 
[[UITapGestureRecognizer alloc] initWithTarget: self 
                                        action: @selector(handleDblTap:)];   
[viewA addGestureRecognizer: dblTap]; 
[viewB addGestureRecognizer: dblTap];

然后期望能够:

-(void)handleDblTap: (UIGestureRecognizer *)gestureRecognizer
{
     if (viewA)...

如果实际上 UIGestureRecognizer 不支持同时附加到多个对象,那么,如果你知道它为什么不支持这个,你能教育我吗?谢谢您的帮助。

4

3 回答 3

22

The standard is one view per recognizer. But you can still efficiently use one handler method.

You would instantiate the recognizers like so:

UITapGestureRecognizer *dblTapViewA = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDblTap:)];   
[viewA addGestureRecognizer: dblTapViewA]; 

UITapGestureRecognizer *dblTapViewB = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDblTap:)];
[viewB addGestureRecognizer: dblTapViewB];

Then your handler method could look something like:

-(void)handleDblTap:(UITapGestureRecognizer *)tapRec{
    if (tapRec.view == viewA){
        // double tap view a
    } else if (tapRec.view == viewB) {
        // double tap view b
    }
}
于 2012-04-24T06:08:22.450 回答
0

您可以分配一个标签来查看,然后简单地比较该标签并执行操作。

UITapGestureRecognizer *dblTap = 
[[UITapGestureRecognizer alloc] initWithTarget: self 
                                    action: @selector(handleDblTap:)];   
[view addGestureRecognizer: dblTap];
view.tag = 2000; // set any integer 

打电话的时候

-(void)handleDblTap:(UITapGestureRecognizer *)tapRec{
    if (tapRec.view.tag == 2000){
      // double tap view with tag
    } 
}
于 2017-01-30T11:45:55.267 回答
-1
UITapGestureRecognizer *dblTapA = 
[[UITapGestureRecognizer alloc] initWithTarget: self 
                                    action: @selector(handleDblTap:)];   
[viewA addGestureRecognizer: dblTapA]; 

UITapGestureRecognizer *dblTapB = 
[[UITapGestureRecognizer alloc] initWithTarget: self 
                                    action: @selector(handleDblTap:)];   
[viewA addGestureRecognizer: dblTapB]; 
于 2012-04-24T06:21:30.400 回答