是否可以同时接收2个类中的UITapGestureRecognizer调用

发布于 2024-09-08 16:56:18 字数 1082 浏览 3 评论 0原文

当用户单击屏幕时,我想在两个类(超级视图和全屏子视图)中调用操作。但是,当我向子视图添加 UITapGestureRecognizer 时,添加到超级视图的 UITapGestureRecognizer 将被覆盖。是否可以将 UITapGestureRecognizer 添加到子视图而不覆盖添加到超级视图的 UITapGestureRecognizer? 如果是这样,我该怎么做?

谢谢!

编辑: 从我的主 viewController“MyToolBerController”中,我从另一个 viewController 添加子视图,如下所示:

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

我在 MyToolBerController 中添加 GestureRecognizer,如下所示:

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

这一切都很好,但是当视图打开时,我需要调用 PhotoViewController 类中的方法以及在 MyToolBerController 类中点击。 当我在 photoViewController 中添加另一个 UITapGestureRecognizer 时,它会覆盖 superView 中添加的 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?

Thanks!

Edit:
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]; 

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];

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦萦几度 2024-09-15 16:56:18

当手势发生时,手势识别器可以调度多个动作。您可以将子视图添加为手势识别器的另一个目标,并且仅使用单个 UITapGestureRecognizer 实例:

[tapRecognizer addTarget:theSubview action:@selector(whatever:)];

Gesture recognizers can dispatch multiple actions when the gesture occurs. You can add the subview as another target of the gesture recognizer and only use a single UITapGestureRecognizer instance:

[tapRecognizer addTarget:theSubview action:@selector(whatever:)];
一个人练习一个人 2024-09-15 16:56:18

在手势识别器选择器方法中,将信息传递到子视图。同一手势不需要多个手势识别器。类似于:

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

这当然意味着需要通知的子视图应该在 Interface Builder 中或在视图控制器加载时在代码中给出标记 100。

根据 Jonah 的代码进行更新:

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

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

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

@property (nonatomic, retain) PhotoViewController *myPhotoViewController;

然后,当您的手势选择器被调用时,传递消息传递到您保留的视图控制器。类似:

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

当然 -doSomethingWithPoint: 方法只是示例。您可以命名并创建任何您想要的方法,该方法接受您想要在 PhotoViewController 中传递的任何参数。

如果您需要进一步说明,请告诉我。

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];
}

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.

Update based on Jonah's code:

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

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

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];
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文