【问题标题】:UIImageView subclass touch detectionUIImageView 子类触摸检测
【发布时间】:2010-03-03 07:06:21
【问题描述】:

我有一个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];
}

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

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

【问题讨论】:

  • 我在这里在黑暗中射击;您是否尝试将其设置为第一响应者?

标签: iphone objective-c cocoa-touch uiimageview touch


【解决方案1】:
self.userInteractionEnabled = NO;

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

【讨论】:

  • 我注释掉了那行并且我的按钮可以工作,但是我不能在系统键盘中使用任何东西。如何“通过”MathKeyboard 将触摸发送到系统键盘?
  • @Cajun:向系统键盘发送消息是“未记录的”。如果目标是文本字段或文本视图,直接修改其text属性。
  • 它不仅没有记录,而且直接不起作用。我必须直接将我的视图添加为键盘的子视图。所以答案并没有直接回答问题,但它让我走上了正确的道路。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-15
  • 2010-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多