0

我的 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我的 GameViewController 的一个属性的子视图这一事实感到困惑。

4

1 回答 1

1

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;那里,因为一些视图是重叠的,它会得到第一个而不是上面的,当然如果你愿意,你可以向后迭代并修复它。

于 2013-07-10T10:06:15.677 回答