好人静
2015-11-22 13:46
采纳率: 50%
浏览 2.3k
已采纳

IOS多个UIView添加点击

刚刚接触IOS开发,遇到以下问题想不通,希望高手给予指导。我给sendView、 payVIew、receiverView、 commentView设置点击事件,为什么
UITapGestureRecognizer * singleRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewClick:)];
singleRecognizer.numberOfTapsRequired = 1;
[self.sendView addGestureRecognizer:singleRecognizer];
[self.payVIew addGestureRecognizer:singleRecognizer];
[self.receiverView addGestureRecognizer:singleRecognizer];
[self.commentView addGestureRecognizer:singleRecognizer];
只有commentView能够响应点击事件,其他的三个UIView都无法响应点击事件,但是我要是改成

UITapGestureRecognizer * singleRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewClick:)];
singleRecognizer.numberOfTapsRequired = 1;
[self.sendView addGestureRecognizer:singleRecognizer];

UITapGestureRecognizer * singleRecognizer1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewClick:)];
singleRecognizer1.numberOfTapsRequired = 1;
[self.payVIew addGestureRecognizer:singleRecognizer1];

UITapGestureRecognizer * singleRecognizer2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewClick:)];
singleRecognizer2.numberOfTapsRequired = 1;
[self.receiverView addGestureRecognizer:singleRecognizer2];

UITapGestureRecognizer * singleRecognizer3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewClick:)];
singleRecognizer3.numberOfTapsRequired = 1;
[self.commentView addGestureRecognizer:singleRecognizer3];
    就可以了呢?希望好心人能够给我这个新手指点迷津
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • Bubble_H 2015-11-23 02:50
    已采纳

    第一种用法相当于创建了一个点击手势 给四个View赋值 它只可以赋值给一个对象 先给sendView赋值 一直执行代码 最后赋值给了commentView,也就是前三个并没有赋值上了,第二种写法就是创建了4个点击手势分别给四个View赋值,就咩有问题了

    点赞 评论
  • devmiao 2015-11-22 18:47
    点赞 评论
  • 一个手势只能给一个view,但是触发方法可以这样,只是手势需要不同的命名!

    点赞 评论
  • Ittttttttta 2015-11-26 12:46

    blockskit ** https://github.com/zwaldowski/BlocksKit**

    [view bk_whenTapped:^{
    }];

    点赞 评论

相关推荐 更多相似问题