为什么触摸事件不会到达我的UIImageView?

时间:2009-07-19 21:24:31

标签: iphone cocoa-touch uikit

我有一些像这样的嵌套视图:

首先,有一个很大的UIView。在这里,还有另一个UIView。在其中,有一个继承自UIView的MyNiceButtons类。所以我有:

UIView> UIView> MyNiceButtons(= UIView)。

详细说明,UIView创建一个UIImageView并将其作为子视图添加到自身,它代表一个图形按钮。所以在MyNiceButtons类实现中,我有这个代码来处理假按钮图像上的触摸事件:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject]; // i just need single touches

    // just to see which view is affected
    UIView *tview = [touch view];
    CGRect rect = tview.frame;
    rect.origin.x = rect.origin.x + 20.0f;
    tview.frame = rect;
    [tview setBackgroundColor:[UIColor yellowColor]];

    if ([touch view] == self.fakeButtonImageView) {
        NSLog(@"touch on fake button detected");
    }
}

层次结构中的所有视图都有userInteractionEnabled = YES。触摸到达层次结构中的第一个视图(父级的父级)。

UIView *tview = [touch view];

这只返回父节点的父节点,但不返回用户实际点击的UIImageView。

如果触摸按钮UIImageView上有什么技巧可以识别?

1 个答案:

答案 0 :(得分:2)

我首先建议您仔细查看使用自定义类型UIButton可以执行的操作(可以为各种状态设置图像)。 95%的时间,这应该满足您的需求。

如果您还有其他需求,我可以帮助您调试w / hitTest等。