如何将 UIImageView 上的触摸限制为其 UIImage 的不透明部分?
我有一个 UIImageView,userInteractionEnabled 设置为 YES,上面有一些手势识别器,可以让用户与其交互。视图的图像是一个形状,其余部分是透明的,我只希望图像的不透明部分接受触摸(超级视图也是交互式的)。
我猜我需要重写命中测试方法,但是是哪一个以及如何重写?
I have a UIImageView with userInteractionEnabled set to YES, and a few gesture recognizers on it which let the user interact with it. The view's image is of a shape, with the rest transparent, and I only want the opaque parts of the image to accept touches (the superview is also interactive).
I'm guessing I need to override a hit testing method, but which one and how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以获取触摸点上的像素颜色信息( 如何获取 iPhone 上图像上像素的 RGB 值)。也许你可以测试它的 alpha 值来确定它是否不透明。
You can obtain the the pixel color information on the touched point ( How to get the RGB values for a pixel on an image on the iphone ). May be you can test for its alpha value to determine whether it is opaque.
尽管这不是您想要的确切答案,但这应该让您了解实现此目标的一种方法。本博客条目使用 Cocoas2D 框架。
http://abitofcode. com/2011/07/irregular-touch-detection-when-cgrect-is-not-enough-part-1/
Although this isn't the exact answer you're after, this should give you an idea of one way of achieving this. This blog entry is using the Cocoas2D framework.
http://abitofcode.com/2011/07/irregular-touch-detection-when-cgrect-is-not-enough-part-1/