你如何知道在 touchesBegan 中被触摸的对象是什么?

How do you tell what object is being touched in touchesBegan?(你如何知道在 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 中被触摸的对象是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)