1

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 に項目を追加するときに、何かおかしなことをしていました。修正しました!

4

1 に答える 1

1

userInteractionEnabledUIImageView のこのプロパティを 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!");
}
于 2013-10-02T18:07:13.583 に答える