我的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的属性。
答案 0 :(得分: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;
,因为有些视图是重叠的,它会得到第一个而不是上面的那个,当然你可以向后迭代并根据需要修复它。