简体   繁体   English

如何将UIImageView上的触摸限制为其UIImage的不透明部分?

[英]How do I restrict touches on a UIImageView to the opaque parts of its UIImage?

I have a UIImageView with userInteractionEnabled set to YES, and a few gesture recognizers on it which let the user interact with it. 我有一个UIImageView,userInteractionEnabled设置为YES,并在其上有一些手势识别器,让用户与之交互。 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? 我猜我需要覆盖一个命中测试方法,但是哪一个以及如何?

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 ). 您可以获取触摸点上的像素颜色信息( 如何获取iphone上图像上像素的RGB值 )。 May be you can test for its alpha value to determine whether it is opaque. 可能是您可以测试其alpha值以确定它是否不透明。

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. 此博客条目使用Cocoas2D框架。

http://abitofcode.com/2011/07/irregular-touch-detection-when-cgrect-is-not-enough-part-1/ http://abitofcode.com/2011/07/irregular-touch-detection-when-cgrect-is-not-enough-part-1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何像iPhoto中一样将UIImageView的滚动限制在UIImage的边界上? - How can I restrict the scrolling of a UIImageView to the bounds of the UIImage like in iPhoto? 如何以编程方式将UIImage添加到UIImageView然后将该UIImageView添加到另一个UIView? - How do I programmatically add a UIImage to a UIImageView then add that UIImageView to another UIView? 如何在UIImageView中显示UIImage? - How can I display a UIImage in a UIImageView? 如何防止触摸通过UIImageView? - How can I prevents touches from passing through a UIImageView? 如何修改我的代码以允许用户在UIImageView上定位UIImage? - How do I modify my code to allow user to position a UIImage on a UIImageView? 如何在子视图中完全限制子视图(UIImageView)? - How restrict a subview(which is a UIImageView) totally in its superview? 如何在不使用UIImageView的情况下缩小UIImage? - How I can pinch zoom UIImage without using UIImageView? 如何在不使用其他UIImage的情况下屏蔽UIImageView,只需简单地定义一个像圆角方形的简单形状? - How do I mask a UIImageView without use of another UIImage, just simply defining a simple shape like a rounded-square? 我如何对UIImage进行去饱和处理? - How do I desaturate a UIImage? 如何获得UIImageView动画停止播放的通知? - How do I get an UIImageView to tell me when its animation stops?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM