简体   繁体   English

UIImageView子类触摸检测

[英]UIImageView subclass touch detection

I have a subclass of UIImageView that I need to know when is touched. 我有一个UIImageView的子类,我需要知道何时触摸它。 (Full code below.) I have found and read both this and this . (下面的完整代码。)我已经找到并阅读了thisthis They were mildly useful, but neither really worked. 它们虽然有用,但是都没有真正起作用。 Not only is my touchesBegan:withEvent: method not being called, the button behind the image is being pressed. 我的touchesBegan:withEvent:方法不仅没有被调用,而且图像后面的按钮也被按下。

Here is my code: 这是我的代码:

Header file: 头文件:

@interface MathKeyboardKey : UIImageView
{

}

@end

Implementation file: 实施文件:

@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  

Calling code: 调用代码:

{
    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];
}

This view is being added as a subview of another (the MathKeyboard ). 该视图被添加为另一个视图( MathKeyboard )的子视图。 The superview cannot have user interaction enabled, as it is covering another view (the system keyboard). 超级视图无法启用用户交互,因为它正在覆盖另一个视图(系统键盘)。 When I attempt to add MathKeyboardKey to the system keyboard view, it doesn't show up. 当我尝试将MathKeyboardKey添加到系统键盘视图时,它没有显示。 If I try to use a UIButton, it never shows up, no matter which view I place it in. 如果我尝试使用UIButton,则无论将其放置在哪个视图中,它都不会显示。

Question, in case it isn't apparent, is: how do I detect touches in this UIImageView subclass? 如果不是很明显,问题是:如何在此UIImageView子类中检测触摸?

self.userInteractionEnabled = NO;

Since you have disabled user interaction of the parent view, how could the child receive any interactions? 由于您已禁用了父视图的用户交互,因此子级将如何接收任何交互? Remove this line. 删除此行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM