カードでアプリを作成しています。私の mainViewController には、次のコードがあります。
CardView *cardView = [[CardView alloc] initWithFrame:CGRectMake(0, 0, CardWidth, CardHeight)];
cardView.card = [player.closedCards cardAtIndex:t];
[self.cardContainerView addSubview:cardView];
[cardView animateDealingToBottomPlayer:player withIndex:t withDelay:delay];
delay += 0.1f;
CardView は UIView のサブクラスです。各 Card は一意の cardView であり、 CardView.m には次のものがあります。
@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;
}
- (void)loadBack
{
if (_backImageView == nil)
{
_backImageView = [[UIImageView alloc] initWithFrame:self.bounds];
_backImageView.image = [UIImage imageNamed:@"Back"];
_backImageView.contentMode = UIViewContentModeScaleToFill;
[self addSubview:_backImageView];
}
}
およびその他の機能の実装。
スペースを獲得するために、1 枚のカードが他のカードの上に置かれるので (カードの半分が表示され、残りは次のカードでカバーされるなど)、各カードのタッチを識別したいと考えています。
そのコードを CardView に配置すると:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Card is touched");
}
呼び出されることはありません。GameView Controller に配置すると、触れる場所ならどこでも呼び出されますが、どの cardView が呼び出されたかを識別する方法がわかりません。ヒントを教えてください。
編集: ジェスチャーを使用することにしました。したがって、私の mainViewController でコードを次のように変更しました。
for (PlayerPosition p = startingPlayer.position; p < startingPlayer.position + 4; ++p)
{
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:)];
[cardView addGestureRecognizer:recognizer];
}
しかし、これは決して呼び出されません。
-(void)cardSelected:(UITapGestureRecognizer *)recognizer
{
NSLog(@"Card Selected with gestures");
}
何故ですか?
編集2:
私もこれを追加しようとしました:
self.cardContainerView.userInteractionEnabled=YES;
またはこれを変更する:
UITapGestureRecognizer *recognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cardSelected:)];
これに:
UITapGestureRecognizer *recognizer=[[UITapGestureRecognizer alloc]initWithTarget:self .cardContainerView action:@selector(cardSelected:)];
しかし、どれも機能しませんでした。