我设置了一个滚动视图和一个UIView长凳(scrollview子视图),上面带有手势:

  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似乎仅影响到最后一个视图。

最佳答案

替换这条线

[self.view addGestureRecognizer:tap];


有了这个

 [view addGestureRecognizer:tap];

关于ios - 多个UIView上的UITapGestureRecognizer(scrollview subview ),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17825498/

10-14 22:00