【问题标题】:UITapGestureRecognizer on multiple UIView (scrollview subviews)多个 UIView 上的 UITapGestureRecognizer(滚动视图子视图)
【发布时间】:2013-07-24 04:56:19
【问题描述】:

我设置了一个滚动视图和一个带有手势的 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 似乎只影响到最后一个视图。

【问题讨论】:

  • [self.view addGestureRecognizer:tap];添加自定义视图而不是主视图

标签: ios uiscrollview uitapgesturerecognizer


【解决方案1】:

替换这一行

[self.view addGestureRecognizer:tap];

有了这个

 [view addGestureRecognizer:tap];

【讨论】:

  • 我确实需要一些咖啡
  • 这只是因为命名转换。你应该给任何对象的正确名称..
猜你喜欢
  • 2023-04-06
  • 2013-11-04
  • 2012-10-25
  • 1970-01-01
  • 2015-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多