我有以下的代碼添加到UITapGestureRecognizer一個UIImageView:的UIImageView GestureRecognizer不工作
UIImageView *circleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"red_circle"]];
[circleView setUserInteractionEnabled:YES];
[circleView setFrame:CGRectMake(20 + 60 * ([self.tasks count] - 1), self.bounds.size.height - 300, 44, 44)];
// register gestures
[self registerGestureRecognizer:circleView];
[self addSubview:circleView];
-(void) registerGestureRecognizer:(UIImageView *) circleView
{
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.delegate = self;
[circleView addGestureRecognizer:tapGestureRecognizer];
}
-(void) tapped:(UITapGestureRecognizer *) recognizer
{
NSLog(@"tapped!");
}
但是,當我觸碰圖像的挖掘方法不會被調用!我錯過了什麼。
包含UIImageView的視圖被添加到UIScrollView。
更新:這裏是增加了一個TaskView到的UIScrollView
代碼-(TaskView *) createTaskView
{
TaskView *taskView = [[TaskView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
taskView.userInteractionEnabled = YES;
taskView.backgroundColor = [UIColor clearColor];
[taskView.textView setDelegate:self];
return taskView;
}
-(void) initializeScrollViewWithTasks
{
for(int day = 1; day <= 7; day++)
{
TaskView *taskView = [self createTaskView];
taskView.tag = day;
// [_taskViews addObject:taskView];
[self.scrollView addSubview:taskView];
}
}
SOLUTION:
完全是我的錯!將項目添加到UIScrollView時,我正在做一些瘋狂的事情。修復!
適合我。您可能需要在'self'上啓用用戶交互,並且可能會查看鏈條。 – Kevin
UIImageView包含在UIView中,UIView添加到UIScrollView中。 –
circleView.userInteractionEnabled = YES;並確保沒有攔截輕擊事件的圖像視圖的UIView前端 –