简体   繁体   English

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

[英]UITapGestureRecognizer on multiple UIView (scrollview subviews)

I have set a scrollview and a bench of UIView (scrollview subviews) with gestures on them: 我已经设置了一个滚动视图和一个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;
    }

The called method when a view is touched: 触摸视图时调用的方法:

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

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

}

So how to fix that? 那么如何解决这个问题呢? UITapGestureRecognizer seems to be affected to the last view only. UITapGestureRecognizer似乎仅受最后一个视图的影响。

replace this line 替换这一行

[self.view addGestureRecognizer:tap];

with this 有了这个

 [view addGestureRecognizer:tap];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM