问题描述
- 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]; 就可以了呢?希望好心人能够给我这个新手指点迷津
解决方案
第一种用法相当于创建了一个点击手势 给四个View赋值 它只可以赋值给一个对象 先给sendView赋值 一直执行代码 最后赋值给了commentView,也就是前三个并没有赋值上了,第二种写法就是创建了4个点击手势分别给四个View赋值,就咩有问题了
解决方案二:
http://jingyan.baidu.com/article/4f7d5712aa92631a201927e8.html
解决方案三:
一个手势只能给一个view但是触发方法可以这样,只是手势需要不同的命名!
解决方案四:
IOS - UIView添加点击事件
IOS---UIView添加点击事件
在UIView中添加多个大小一样的框框 (小View)
解决方案五:
blockskit ** https://github.com/zwaldowski/BlocksKit**
[view bk_whenTapped:^{
}];
时间: 2024-10-30 05:54:55