如何判断在touchesBegan中哪个对象被触摸了?
我知道这是一个非常常见的问题,但每个网站上的所有答案都不起作用!如果你还是不明白我的意思,那么也许这行代码会帮助你理解。
- (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:
而不是 ==
,但这也没有成功。有什么建议吗?
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)];
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须为所有 UIImageView 设置 userinteractionEnabled = YES,否则它们将不会收到触摸事件。还要更改行:
至
You have to set userinteractionEnabled = YES for all your UIImageViews otherwise they will not receive touch events. Also change the line:
to
我创建了一个检查,以确保它是我希望在继续之前单击的视图。
I created a check to be sure its the view I expect to be clicked before going on.