我有以下代码将UITapGestureRecognizer添加到UIImageView:
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];
}
}
解决方案:
完全是我的错!在向UIScrollView添加项目时,我正在做一些疯狂的事情。固定它!
答案 0 :(得分:1)
您需要将UIImageView的此属性userInteractionEnabled
设置为YES。像这样替换你的代码:
-(void) registerGestureRecognizer:(UIImageView *) circleView
{
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
tapGestureRecognizer.delegate = self;
tapGestureRecognizer.numberOfTapsRequired = 1;
circleView.userInteractionEnabled=YES;
[circleView addGestureRecognizer:tapGestureRecognizer];
}
-(void) tapped:(UITapGestureRecognizer *) recognizer
{
NSLog(@"tapped!");
}