1

我有一个UIImageView我需要知道什么时候被触摸的子类。(下面的完整代码。)我已经找到并阅读了thisthis。它们有点用处,但都没有真正起作用。不仅touchesBegan:withEvent:没有调用我的方法,而且还按下了图像后面的按钮。

这是我的代码:

头文件:

@interface MathKeyboardKey : UIImageView
{

}

@end

实现文件:

@implementation MathKeyboardKey

- (id)initWithImage:(UIImage *)image
{
    if (self = [super initWithImage:image])
    {
        //init here
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"-- I AM TOUCHED --");
}

- (void)dealloc
{
    [super dealloc];
}

@end  

调用代码:

{
    self.userInteractionEnabled = NO;

    mathKeyboardAccess = [[MathKeyboardKey alloc] initWithImage:[UIImage imageNamed:@"mathKey.png"]];
    CGRect frm = mathKeyboardAccess.frame;
    frm.origin = CGPointMake(80, 171);
    mathKeyboardAccess.frame = frm;
    mathKeyboardAccess.userInteractionEnabled = YES;
    [self addSubview:mathKeyboardAccess];
}

此视图被添加为另一个 (the MathKeyboard) 的子视图。超级视图不能启用用户交互,因为它覆盖了另一个视图(系统键盘)。当我尝试添加MathKeyboardKey到系统键盘视图时,它没有出现。如果我尝试使用 UIButton,无论我将它放在哪个视图中,它都不会出现。

如果不明显,问题是:我如何检测这个UIImageView子类中的触摸?

4

1 回答 1

1
self.userInteractionEnabled = NO;

由于您已禁用父视图的用户交互,孩子如何接收任何交互?删除此行。

于 2010-03-03T13:08:09.077 回答