简体   繁体   English

识别对在iOS中以编程方式创建的UIViews子类的触摸

[英]identify touches on subclass of UIViews created programmatically in iOS

I am creating an app with cards. 我正在用卡片创建一个应用程序。 In my mainViewController, I have this code: 在我的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;

where CardView is a subclass of UIView. 其中CardView是UIView的子类。 Each Card is a unique cardView and in CardView.m I do have: 每张卡都是唯一的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];
    }
}

and the implementations for other functions . 以及其他功能的实现。

Since in order to win space, one card is placed on top of the others (half of teh card is visible and the rest is covered by the next card and so on), I want to identify touches on each card. 由于为了赢得空间,一张卡被放置在另一张卡的顶部(该卡的一半是可见的,其余卡被下一张卡覆盖,依此类推),因此我想确定每张卡上的触感。

If I place that code in CardView: 如果我将代码放在CardView中:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Card is touched");
}

it is never called. 它永远不会被调用。 If I place it in the GameView Controller it is called anywhere that I will touch, but I do not know how to identify which cardView is called. 如果将其放置在GameView Controller中,它将在我将触摸的任何地方被调用,但是我不知道如何识别哪个cardView被调用。 Can you give me a hint? 你能给我一个提示吗?

EDIT: I decided to use gestures. 编辑:我决定使用手势。 Therefore in my mainViewController changed the code to this: 因此在我的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];
}

but this is never called. 但这从未被称为。

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

Why is that? 这是为什么?

EDIT 2: 编辑2:

I have also tried adding this: 我也尝试添加以下内容:

self.cardContainerView.userInteractionEnabled=YES; 

or changing this: 或更改此:

UITapGestureRecognizer *recognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cardSelected:)];

to this: 对此:

UITapGestureRecognizer *recognizer=[[UITapGestureRecognizer alloc]initWithTarget:self .cardContainerView action:@selector(cardSelected:)];

but none of them worked. 但他们都不起作用。

If you instead add a tap gesture recogniser to each card view you can get a callback to a method you specify and the gesture is connected to the view so you can directly get a reference to it. 如果改为在每个卡片视图中添加轻击手势识别器,则可以获取指定方法的回调,并且手势已连接到该视图,因此您可以直接获取对其的引用。


Your CardView (which I guess is a subclass of UIView ?) has 2 subviews, which are image views: 您的CardView (我想是UIView的子类?)有2个子视图,它们是图像视图:

UIImageView *_backImageView;
UIImageView *_frontImageView;

You may want to set userInteractionEnabled to YES on one or both of them (depending on when the card should be tappable and when the subviews are shown and hidden). 您可能要在其中一个或两个上都将userInteractionEnabled设置为YES (取决于何时应点击卡以及何时显示和隐藏子视图)。

You can also act as the delegate of the gesture (if you need to, or just temporarily to debug that the gesture is getting triggered but blocked by something other gesture). 您还可以充当手势的委托人(如果需要,或者只是暂时调试手势已被触发但被其他手势阻止)。

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

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