ios - 多个 UIView( ScrollView subview )上的 UITapGestureRecognizer

标签 ios uiscrollview uitapgesturerecognizer

我已经设置了一个 ScrollView 和一个带有手势的 UIView ( ScrollView subview ):

  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;
    }

触摸 View 时调用的方法:

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

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

}

那怎么解决呢? UITapGestureRecognizer 似乎只影响最后一个 View 。

最佳答案

替换这一行

[self.view addGestureRecognizer:tap];

有了这个

 [view addGestureRecognizer:tap];

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

相关文章:

ios - swift 中嵌套闭包内的位置引用的闭包参数

ios - UIScrollView/向其中添加 xib

ios - 你如何在 ScrollView 中居中显示内容?

ios - swift : How to set two action on Single Gesture

iphone - 数据未传递给 View

ios - 在框架中覆盖 Storyboard中的图像

ios - 没有委托(delegate)方法的缩放 UIScrollView 的子类

iphone - 点击 UITabbar 按钮上的手势

ios - 为什么这个 UITapGestureRecognizer 无法识别点击?

ios - 在stringWithContentsOfFile期间记录错误消息