iPhone SDK:检测被点击的视图
在我的应用程序中,我有很多小的 UIImageView 和侧面的视图。我需要的是侧面的视图,以便能够检测到哪个图像视图被点击,并为我提供有关它的所有信息(很像 (id)sender)及其坐标。最好的方法是什么?
In my application I have a lot of little UIImageViews and a view off to the side. What I need is that view off to the side to be able to detect which image view was tapped and give me all the information about it (much like with (id)sender) as well as its coordinates. What is the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我的项目的内部类。我想你明白了。
作为一个选项,您可以为所有者(或委托人)创建协议。
获取坐标:
您可以使用以下代码
This is the internal class from my project. I think you get the idea.
As an option you can create protocol for the owner (or delegate).
You can obtain coords using
Here is the code:
我将继承
UIImageView
并为您想要发送消息的目标视图添加一个新属性。然后重新启用userInteractionEnabled
并向touchesUpInside添加一个操作。在自定义子类的操作方法中调用目标视图上的方法,您还可以在其中提供自定义子视图的对象。基本上,您使用委托并传入委托调用您需要的所有参数。
I would subclass
UIImageView
and add a new property for the target view you would like to message. Then reenableuserInteractionEnabled
and add a action to touchesUpInside.In the action method of the custom subclass call a method on the target view in which you also give the object of the custom subview. Basically you use delegation and pass in the delegate call all parameters you need.
将标签添加到 UIImageView,如 myUIImageView.tag = intNumber;
在 side TouchsBegan 中,或者您可以为所有 UIImageView 调用任何通用方法,并使用标签来识别哪个视图被点击。
Add tag to UIImageView like myUIImageView.tag = intNumber;
and in side touchesBegan or you can call any common method for all your UIImageView and use tag to identify which view is tapped.