是否可以同时接收2类的UITapGestureRecognizer调用 [英] Is it possible to receive UITapGestureRecognizer calls in 2 classes at the same time

查看:54
本文介绍了是否可以同时接收2类的UITapGestureRecognizer调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单次点击屏幕时,我想在两个类(一个超级视图和一个全屏子视图)中调用一个动作.但是,当我将UITapGestureRecognizer添加到子视图时,将覆盖添加到父视图的那个视图.是否可以将UITapGestureRecognizer添加到子视图而不会覆盖添加到父视图的UITapGestureRecognizer? 如果是这样,我该怎么办?

I want to call an action in two classes (a superview and a full screen subview) when the user single taps the screen. But, when I add a UITapGestureRecognizer to the subview, the one added to the superview is overridden. Is it possible to add a UITapGestureRecognizer to a subview without overriding the UITapGestureRecognizer added to the superview? If so, how can I do this?

谢谢!

修改: 从我的主viewController"MyToolBerController",我从另一个viewController中添加子视图,如下所示:

From my main viewController "MyToolBerController", I'm adding the subview from another viewController as follows:

PhotoViewController *photoViewController = [[PhotoViewController alloc] initWithNibName:@"PhotoViewController" bundle:nil];
myPhotoView = photoViewController.view;
[self.view addSubview:myPhotoView]; 

我像这样在MyToolBerController中添加GestureRecognizer:

I add the GestureRecognizer in the MyToolBerController like this:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom:)];        
[singleTap setNumberOfTapsRequired:1];
singleTap.delegate = self;
[myPhotoView addGestureRecognizer:singleTap];
[singleTap release];

这一切都很好,但是当视图被点击时以及MyToolBerController类中,我需要在PhotoViewController类中调用一个方法. 当我在photoViewController中添加另一个UITapGestureRecognizer时,它将覆盖在superView中添加的UITapGestureRecognizer.

This all works fine, but I need to call a method in the PhotoViewController class when the view is tapped as well as in the MyToolBerController class. When I add another UITapGestureRecognizer in the photoViewController, it overrides the UITapGestureRecognizer added in the superView.

推荐答案

在手势识别器选择器方法中,将信息传递给子视图.相同的手势不需要多个手势识别器.像这样:

In your gesture recognizer selector method, pass the information along to the subview. There's no need to have multiple gesture recognizers for the same gesture. Something like:

- (IBAction)handleSingleDoubleTap:(UIGestureRecognizer *)sender
{
    CGPoint tapPoint = [sender locationInView:sender.view.superview];
    UIView *subview = [parentView viewWithTag:100];
    [subview doSomethingWithPoint:tapPoint];
}

这当然意味着在加载视图控制器时,应该在Interface Builder或代码中为需要通知的子视图赋予标签100.

This of course means that your subview that needs to be notified should be given the tag 100 either in Interface Builder or in code when the view controller gets loaded.

基于乔纳的代码进行更新:

因此,不要保留视图,而要保留视图控制器:

So instead of retaining the view, retain the view controller:

PhotoViewController *photoViewController = [[PhotoViewController alloc] initWithNibName:@"PhotoViewController" bundle:nil];
self.myPhotoViewController = photoViewController;

这意味着您需要在MyToolbarController标头中以这种方式声明它:

Which means you need to declare it this way in the MyToolbarController header:

@property (nonatomic, retain) PhotoViewController *myPhotoViewController;

然后,当手势选择器被调用时,将消息传递给您保留的视图控制器.像这样:

Then, when your gesture selector gets called, pass the message along to the view controller you retained. Something like:

- (IBAction)handleSingleTapFrom:(UIGestureRecognizer *)sender
{
    CGPoint tapPoint = [sender locationInView:sender.view.superview];
    [myPhotoViewController doSomethingWithPoint:tapPoint];
}

当然,-doSomethingWithPoint:方法仅作为示例.您可以命名并创建任何想要的方法,该方法可以使用要在PhotoViewController中传递的任何参数.

Of course the -doSomethingWithPoint: method is only for example. You can name and create any method you want that takes any parameter you want to pass in your PhotoViewController.

让我知道是否需要进一步说明.

Let me know if you need further clarification.

这篇关于是否可以同时接收2类的UITapGestureRecognizer调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆