繁体   English   中英

uitapgesturerecognizer在子视图iOS上不起作用

[英]uitapgesturerecognizer does not work on subviews iOS

我使用以下代码将mainViewController名称命名为GameViewController:

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

CardView是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;
}

由于空间有限,这些卡一张一张放置在另一张上方,例如可以看到一半的卡,其余的则从顶部的卡盖住,依此类推。

我希望能够识别出按下了哪张卡。

但是,在我的mainViewController中,我确实具有以下功能:

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

但它从未被调用。

您能帮忙解决什么吗? 可能有一些视图阻止了触摸或其他操作,但是我无法弄清楚哪一个。 我对CardViews作为self.cardContainerView的子self.cardContainerView添加的事实感到困惑,这是我的self.cardContainerView的属性。

GameViewController您可以添加以下代码:

-(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);
}

当然,您可以用0消除这些值,其余的就是卡。

我没有break; 那里是因为某些视图是重叠的,它将得到第一个而不是上面的视图,当然,您可以向后迭代并根据需要进行修复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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