iOS 多个view添加手势, 识别是哪个view的事件

当多个view添加手势识别时, 想要区分是哪个view发生的事件, 那么可以这样获得

- (void)setupViews{
CGFloat menuviewTop;
for (int i = 0; i < 5; i++) {
    menuviewTop = i * 52;
    UILabel *label = [[UILabel alloc]init];
    label.tag = i;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(menuViewClick:)];
    [tap setNumberOfTapsRequired:1];
    [tap setNumberOfTouchesRequired:1];
    [label addGestureRecognizer:tap];
    [self.view addSubview:label];
}
}

识别点击代码:

- (void)menuViewClick:(UIPanGestureRecognizer *)recognizer{
       UITapGestureRecognizer *tap = (UITapGestureRecognizer *)recognizer;
       NSInteger tag = tap.view.tag;
      switch (tag) {
          case 0:{
              break;
          }
          case 1:{
              break;
          }      
          default:
              break;
      }
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 UIScrollView添加手势识别器时,有可能会出现手势冲突的情况。这通常是由于 UIScrollView 自身的手势识别器和添加手势识别器之间的冲突引起的。 要解决这个问题,有几种可能的方法: 1. 禁用 UIScrollView手势识别器。这可以通过将 UIScrollView 的属性 `canCancelContentTouches` 和 `delaysContentTouches` 设置为 `NO` 来实现。但是,这样会使 UIScrollView 的滚动功能也被禁用,因此在某些情况下可能并不可取。 2. 使用手势识别器代理方法来控制手势冲突。在手势识别器代理方法 `gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:` 中,可以根据需要返回 `YES` 或 `NO`,以允许或禁止多个手势识别器同时识别手势。 3. 将手势识别添加到 UIScrollView 的子视图中,而不是直接添加到 UIScrollView 上。这样,手势识别器就不会与 UIScrollView手势识别器冲突了。 下面是一个示例代码,演示了如何在 UIScrollView添加一个 UISwipeGestureRecognizer 手势识别器,并处理手势冲突的情况: ```objc // 创建一个 UISwipeGestureRecognizer 手势识别器 UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; // 设置手势方向 swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; // 将手势识别添加到 UIScrollView 上 [scrollView addGestureRecognizer:swipeGestureRecognizer]; // 实现手势识别器代理方法,处理手势冲突 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] && [otherGestureRecognizer.view isKindOfClass:[UIScrollView class]]) { return NO; // 禁止 UISwipeGestureRecognizer 和 UIScrollView 同时识别手势 } return YES; } // 处理 UISwipeGestureRecognizer 手势 - (void)handleSwipe:(UISwipeGestureRecognizer *)gestureRecognizer { // 处理手势事件 } ``` 希望这些提示可以帮助你解决 UIScrollView 上的手势冲突问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值