我有一个类Player,它是UIImageView的子类。它们由另一个类产生并在屏幕上停留3秒钟然后消失。如果用户点击图像,它应该立即消失。不幸的是,touchesBegan
永远不会被解雇。我确实将userInteractionEnabled设置为YES。
-(id)initWithImage:(UIImage *)image
{
self = [super initWithImage:image];
if (self) {
// Initialization code here.
[self setUserInteractionEnabled:YES];
// start timers to have it disappear after 3 sec
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// never called
[self disappear:nil];
}
我能找到的所有答案都说将userInteractionEnabled设置为YES,但我已经完成了。如果它意味着什么,那么这些图像是由另一个名为Manager的类生成的,它将自己的UIImageView作为应用程序的背景。这些图像较小,并在该背景上定期闪烁。后台UIImageView还将userInteractionEnabled设置为YES。图像闪烁没问题,他们只是不响应触摸事件。
Player类是由Manager类生成的,如下所示:
Player* t = [[Player alloc] initWithImage:[imgArray objectAtIndex:imageIndex]];
[field addSubview:t];
[t becomeFirstResponder];
field
是背景图片(也是UIImageView)。
答案 0 :(得分:0)
编辑:非UIGestureRecognizer版本:
-(id)initWithImage:(UIImage*)image
{
if (( self = [ super initWithImage:image ] ))
{
UIControl * control = [ [ [ UIControl alloc ] initWithFrame:self.bounds ] autorelease ] ;
[ button addTarget:self action:@selector( wasTapped ) forControlEvents:UIControlEventTouchUpInside ] ;
[ self addSubview:control ];
}
}
您可以尝试使用UIGestureRecognizer,即...即
-(id)initWithImage:(UIImage*)image
{
if (( self = [ super initWithImage:image ] ))
{
...
UIGestureRecognizer * recognizer = [ [ [ UITapGestureRecognizer alloc ] initWithTarget:self action:@selector( myTapHandler: ) ] autorelease ] ;
[ self addGestureRecognizer:recognizer ] ;
...
}
}
答案 1 :(得分:0)
问题确实是字段UIImageView需要将userInteractionEnabled设置为YES。我之前意识到这一点,但是已经在init方法中设置了它。当我将它移动到awakeFromNib时,一切正常:)