[英]What can be the reason that my UIImageView subclass doesn't receive any touch events?
[英]UIImageView subclass touch detection
我有一個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
子類中檢測觸摸?
self.userInteractionEnabled = NO;
由於您已禁用了父視圖的用戶交互,因此子級將如何接收任何交互? 刪除此行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.