ios子视图没有响应触摸事件

时间:2014-02-08 02:16:12

标签: ios uiview

我正在尝试为我的一个视图控制器创建一个基本的卡片翻转动画。这是当前的设置:

我有基本的ViewController,这个视图控制器有一个带有子视图的XIB,名为containerView(如下所示)。

enter image description here

这个容器视图基本上保持了翻转的“卡片”的两种状态。我有一个UIView子类来表示cardFront和cardBack。在ViewController中,我在viewDidLoad中有一些代码来获取对两个视图的引用:

ViewController.m

cardFront= [[UICard alloc]initWithType:YES];
cardBack = [[UICard alloc]initWithType:NO];
containerView.userInteractionEnabled = YES;   
[containerView addSubview:cardBack];

然后,我基本上只需用小动画添加和删除子视图到containerView。

然后,我有两个XIB,一个用于cardFront,一个用于cardBack,UICard决定哪个视图要膨胀。所以UICard看起来像这样:

UICard.m

-(id)initWithType:(BOOL)front{
    self = [super init];
    self.isFront = front;

    if(isFront){
        [[NSBundle mainBundle] loadNibNamed:@"CardFront" owner:self options:nil];
    }
    else{
        [[NSBundle mainBundle] loadNibNamed:@"CardBack" owner:self options:nil];
    }
    [self addSubview:self.view];


    self.view.userInteractionEnabled = YES;



    return self;
}
//..later on, when i want to flip the card
[UIView transitionWithView:containerView
                          duration:0.5
                           options:UIViewAnimationOptionTransitionFlipFromLeft
                        animations:^
                        {
                            [cardFront removeFromSuperview];
                            [containerView addSubview:cardBack];
                            cardFront.isShowing = NO;
                            cardBack.isShowing = YES;
                            isFlipping = NO;
                        }
                        completion:NULL];

非常直接。问题是,我的UICard视图(正面或背面)没有注册任何点击。我有按钮等,只是不接受任何用户交互。为了测试,我已经放置了

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

-(UIView *) hitTest:(CGPoint)point withEvent:(UIEvent *)event{

在UICard.m中,在UICard类中都没有被调用。所有我能想到的是,不知何故,containerView正在窃取所有的触摸事件,但在iOS(我相信)中,最低的子视图首先获得点击。任何人都对这里可能发生的事情有任何想法吗?

编辑: 这是cardBack.xib的图片。我将视图(文件所有者)连接到UICard的视图属性

enter image description here

1 个答案:

答案 0 :(得分:1)

在您的UICard类中尝试self.userInteractionEnabled = YES;,因为self是您真正希望启用用户互动的实际视图。