如何检测 UIScrollview 中 UIImageView 的触摸?
我的应用程序中有一个 UIScrollview,我用大量大约 900 个 UIImageView 填充它。它们都非常小,并且只包含两个不同的图像。
现在,我在检测对这些 UIImageView 之一的触摸时遇到了很多麻烦。
我为它们分配了一个独特的标签,以便能够区分它们,但我真的很难检测到触摸。
目标只是能够更改触摸的 UIImageView 的图像。
由于涉及大量视图,针对每个 UIImageViews 框架检查触摸坐标的简单循环只是挂起我的应用程序。
有人有什么建议吗?
提前致谢。
本
I have a UIScrollview in my app and I populate it with LOTS of UIImageViews approx 900. They are all very small and consist of only two different images over and over again.
Now I am having a lot of trouble detecting a touch on one of these UIImageViews.
I have assigned them all a unique TAG so as to be able to distinguish between them but I am really struggling to detect the touch.
The goal is just to be able to change the image of the touched UIImageView.
Due to the large amount of views involved a simple loop checking touch coordinates against each UIImageViews frame is just hanging my app.
Does anyone have any suggestions?
Thanks in Advance.
Ben
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
就我而言,最简单的方法是添加手势识别器:
然后使用此方法来捕获触摸:
In my case the easiest way to do it was adding a gesture recognizer:
Then use this method to capture the touch:
UIImageView 默认关闭触摸处理。要将 userInteractionEnabled 设置为 YES(请参阅此处的文档 http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIImageView_Class/Reference/Reference.html)
此外,如果您想知道在视图层次结构中哪个视图被点击可以使用
hitTest:withEvent:
(请参阅 UIView 中的文档),这比循环遍历层次结构要快得多。从更广泛的角度来看,我认为屏幕上同时显示 900 个 UIImageView 并不是一个好的长期策略。您是否研究过通过 CoreGraphics 绘制屏幕内容?
UIImageView has touch processing turned off by default. To change that set userInteractionEnabled to YES (see the docs here http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIImageView_Class/Reference/Reference.html)
Also if you want to know which view was hit in a view hierarchy you can use
hitTest:withEvent:
(see docs in UIView) which will be much faster than you looping through the hierarchy.On a wider note I don't think having 900 UIImageView's on the screen all at once is going to be a good long term strategy. Have you investigated drawing the screen's content via CoreGraphics?
解决这个问题的最好方法是子类化 UIScrollView,为其添加 TouchBegan 等方法。
The best way to solve this issue is to subclass UIScrollView, add touchesBegan etc methods to it.
您可以轻松地在 UIImageView 对象上实现此方法:
使用返回值告诉应用程序下一个触摸响应程序(例如 uiimageview 或滚动视图)。
You could easily implement this method on the UIImageView object:
Use the return to tell the application the next touch responder (such as the uiimageview or the scrollview).