2

I have an array of similar objects (thumbnails) on which I appended a singleFingerTap recognizer like this:

UITapGestureRecognizer *singleFingerTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundThumbnailTapped)];
    [self addGestureRecognizer:singleFingerTap];

How can I know in my selector (backgroundThumbnailTapped) which of the thumbnails was tapped?

4

1 回答 1

3

Change the signature of your selector to accept the gesture recognizer, like this:

-(void) backgroundThumbnailTapped:(UIGestureRecognizer *)gestureRecognizer {
    // Below, you can get the view to which the recognizer is attached:
    [gestureRecognizer.view doSomething];
    //                 ^^^
    //                  |
    //     This is the view in which the tap was triggered
}

You need to put a colon when adding the recognizer:

UITapGestureRecognizer *singleFingerTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self
        action:@selector(backgroundThumbnailTapped:)];
//                                 Here ----------^
    [self addGestureRecognizer:singleFingerTap];
于 2013-03-30T13:53:19.733 回答