多个UIView上的UITapGestureRecognizer(scrollview子视图)

时间:2013-07-24 04:56:20

标签: ios uiscrollview uitapgesturerecognizer

我设置了一个滚动视图和一个UIView(滚动视图子视图)的工作台,上面有手势:

  for (id element in array) {

        CustomView *view = [[CustomView alloc] init];
        [view setFrame:CGRectMake(x, 16, self.view.frame.size.width, self.view.frame.size.height)];
        [self.scrollView setContentSize:CGSizeMake(scrollContentSizeWidth, self.scrollView.frame.size.height)];
        UITapGestureRecognizer *tap =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(selectView:)];
        [self.view setTag:[[element valueForKey:@"Id"] integerValue]];
        [self.view addGestureRecognizer:tap];
        view.userInteractionEnabled = YES;
        [self.scrollView addSubview:view];
        scrollContentSizeWidth +=110;
        x += 110;
    }

触摸视图时的被调用方法:

-(void)selectView:(UITapGestureRecognizer *)recognizer{

    NSLog(@"id : %i",recognizer.view.tag);//always last assigned value in the loop above

}

那么如何解决这个问题呢? UITapGestureRecognizer似乎仅受最后一个视图的影响。

1 个答案:

答案 0 :(得分:2)

替换此行

[self.view addGestureRecognizer:tap];

用这个

 [view addGestureRecognizer:tap];