简体   繁体   中英

uitapgesturerecognizer does not work on subviews iOS

I am having my mainViewController names as GameViewController with the following code:

@interface GameViewController : UIViewController <UIAlertViewDelegate, GameDelegate,UIGestureRecognizerDelegate>

@property (nonatomic, weak) IBOutlet UIView *cardContainerView;


... (the following code is in a function called -DealCards)

for (PlayerPosition p = startingPlayer.position; p < startingPlayer.position + 4; ++p)
{
    Player *player = [self.game playerAtPosition:p % 4];
    CardView *cardView = [[CardView alloc] initWithFrame:CGRectMake(0, 0, CardWidth, CardHeight)];
    cardView.card = [player.closedCards cardAtIndex:t];
    cardView.userInteractionEnabled=YES;
    [self.cardContainerView addSubview:cardView];
    [cardView animateDealingToBottomPlayer:player withIndex:t withDelay:delay];
    delay += 0.1f;
    UITapGestureRecognizer *recognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cardSelected:)];
    [recognizer setDelegate:self];
    [cardView addGestureRecognizer:recognizer];
}

The CardView is a subclass of UIView:

@implementation CardView
{
    UIImageView *_backImageView;
    UIImageView *_frontImageView;
    CGFloat _angle;
}

@synthesize card = _card;

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
    {
        self.backgroundColor = [UIColor clearColor];
        [self loadBack];
        self.userInteractionEnabled=YES;
    }
    return self;
}

Because of limited space, the cards are placed one on top of the other, like the half card is visible and the rest is covered from the card on top and so on.

I want to be able to identify which card is pressed.

However, in my mainViewController I do have this function:

-(void)cardSelected:(UITapGestureRecognizer *)recognizer
{
    NSLog(@"Card Selected with gestures");
}

but it is never called.

Can you help with what is missing? There might be some view that blocks the touches or something but I cannot figure out which one. I am confused by the fact that CardViews are added as a subView of self.cardContainerView which is a property of my GameViewController.

In GameViewController you add this :

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[touches allObjects] objectAtIndex:0];
    CGPoint touchLocation = [touch locationInView:self.cardContainerView];
    CardView *selectedCard;
    for (CardView *card in self.cardContainerView.subviews)
    {
    if(CGRectContainsPoint(card.frame, touchLocation))
     {
         selectedCard = card;

     }
    }

    NSLog(@"Value %d",selectedCard.card.value);
}

Of course you eliminate the values with 0 and the rest are the cards.

I haven't placed a break; there because some of the views are overlapping and it will get the first one instead of the one above, of course you could iterate backwards and fix that if you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM