How do you tell what object is being touched in touchesBegan?(你如何知道在 touchesBegan 中被触摸的对象是什么?)
问题描述
我知道这是一个非常常见的问题,但是每个网站上的所有答案都不起作用!如果你还是不明白我的意思,那么也许这行代码可以帮助你理解.
I know that this is a very commonly asked question, but all of the answers on every website don't work! If you still don't know what I mean, then maybe this line of code will help you understand.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if (touch.view == nextbutton)
[self performSelector:@selector(next)];
if (touch.view == prevbutton)
[self performSelector:@selector(previous)];
if (touch.view == moreoptionsbutton)
[self performSelector:@selector(moresettings)];
}
当你触摸 nextbutton、prevbutton 和 more optionsbutton
时,它不会做任何事情,顺便说一下,它们是 UIImageViews
.我也尝试过使用 isEqual:
而不是 ==
,但这也没有成功.有什么建议吗?
It doesn't do anything when you touch nextbutton, prevbutton, and more optionsbutton
, which are UIImageViews
by the way. I have also tried using isEqual:
instead of ==
, but that hasn't worked out either. Any suggestions?
推荐答案
你必须为你所有的 UIImageViews 设置 userinteractionEnabled = YES 否则他们将不会收到触摸事件.同时换行:
You have to set userinteractionEnabled = YES for all your UIImageViews otherwise they will not receive touch events. Also change the line:
UITouch *touch = [[event allTouches] anyObject];
到
UITouch *touch = [touches anyObject];
这篇关于你如何知道在 touchesBegan 中被触摸的对象是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:你如何知道在 touchesBegan 中被触摸的对象是什么?
![](/xwassets/images/pre.png)
![](/xwassets/images/next.png)
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01