【发布时间】:2010-03-03 07:06:21
【问题描述】:
我有一个UIImageView 的子类,我需要知道它何时被触摸。 (下面的完整代码。)我找到并阅读了this 和this。它们有点用处,但都没有真正起作用。不仅没有调用我的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];
}
这个视图被添加为另一个(MathKeyboard)的子视图。超级视图不能启用用户交互,因为它覆盖了另一个视图(系统键盘)。当我尝试将MathKeyboardKey 添加到系统键盘视图时,它没有出现。如果我尝试使用 UIButton,无论我将它放在哪个视图中,它都不会出现。
如果不明显,问题是:我如何在这个 UIImageView 子类中检测触摸?
【问题讨论】:
-
我在这里在黑暗中射击;您是否尝试将其设置为第一响应者?
标签: iphone objective-c cocoa-touch uiimageview touch