开发者

Is it possible to receive UITapGestureRecognizer calls in 2 classes at the same time

开发者 https://www.devze.com 2023-01-06 01:03 出处:网络
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 supe

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!

Edi开发者_运维知识库t: 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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号