点击检测不能在UIImageView上工作

在我的.m文件中我添加了:

@property (strong, nonatomic) UIImageView *star1; 

然后用我做的一个方法:

 UIImage *star1Image; star1Image = [UIImage imageNamed:@"staryes"]; self.star1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)]; self.star1.tag = 800; [self.star1 setImage:star1Image]; [ratingLabelBody addSubview:self.star1]; 

几行后,没有涉及到我有:

 [self.star1 setUserInteractionEnabled:YES]; UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgTouchUp:)]; tapped.numberOfTapsRequired = 1; [self.star1 addGestureRecognizer:tapped]; 

最后在我已经实现的.m文件中:

 -(void)imgTouchUp:(id)sender { NSLog(@"imgTouchUp"); UITapGestureRecognizer *gesture = (UITapGestureRecognizer *)sender; NSLog(@"tap detected on %li", (long)gesture.view.tag); } 

所有这一切,它应该认识到我的形象上的水龙头,但没有发生。 任何想法?

因此,由于像UILabelUIImageView这样的组件不是devise为“可触摸的”,所以要添加一个“可触摸的”function(如UITapRecognizer ),您必须将其userInteractionEnabled设置为YES

所以,即使你正确地为你的UIImageViewstar1 )设置了这个属性,既然你将它作为ratingLabelBody的子视图添加,你将无法触发你的UITapGestureRecognizerimgTouchUp:
您必须对您的star1的父级视图做相同的star1 ,这是ratingLabelBody

用代码翻译,你只需要做:

 [ratingLabelBody setUserInteractionEnabled:YES];