UIImageView 不同区域的 UIGesture 识别 [英] UIGesture recognition on different areas of a UIImageView

查看:23
本文介绍了UIImageView 不同区域的 UIGesture 识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这张图片:

我想要做的是向该图像添加一个 UITapGestureRecognizer(或者我可以将图像拆分为它所组成的不同部分,并为每个部分添加一个 UITapGestureRecognizer),以便根据点击的叶子执行不同的操作.如果我将图像拆分为每个叶子的不同图像,UIImageViews 可能会重叠,点击一个将被识别为点击另一个.只有一张图片意味着知道屏幕上属于一片叶子而不是另一片叶子的点.

What I want to do is to add a UITapGestureRecognizer to this image (or I can split the image in the different parts it consists of and add for each part a UITapGestureRecognizer) in order to have different actions according to the leaf tapped. If I split the image in different images each for each leaf the UIImageViews will probably overlap and tapping on one will be recognized as a tap on another one. Having just one image implies knowing the points of the screen that belongs to a leaf rather than to another one.

任何有关如何做到这一点的线索将不胜感激.谢谢

Any clues on how to do it would be really appreciated. Thanks

推荐答案

通过检查手势识别器的 locationInView: 来改变你的行为.

Change your behavior by examining the gesture recognizer's locationInView:.

如果您将图像作为一个单元处理,请在您的手势识别器回调中实现这一点,以确定点击了哪个叶子"(如果有).

If you handle the image as one unit, implement this in your gesture recognizer call back to decide which "leaf" (if any) was tapped.

如果您将图像作为多个图像处理,您也可以在回调中实现它,或者您也可以在例如您的委托的 gestureRecognizerShouldBegin: 中实现以抑制叶子外部的触摸事件,如绘制.

If you handle the image as multiple images, you could also implement it in your callback, or you could also implement in, e.g., your delegate's gestureRecognizerShouldBegin: to suppress events for touches outside the leaf as drawn.

我没有意识到您可能也在寻求帮助来确定一个点是否位于叶子内.@PhillipMills 在这一点上是正确的:我们需要知道您是如何绘制图像的.

I didn't realize that you might also be looking for assistance on figuring out whether a point lies within a leaf. @PhillipMills is correct on this point: we need to know how you are drawing the image.

跟进:这有点超出我的专业领域.

FOLLOW-UP: This is somewhat outside my area of expertise.

最简单的方法(从命中测试的角度来看)是按照@PhillipMills 的建议进行操作,使用 Quartz 绘图和 CGPathContainsPoint().如果您有需要渲染为 PNG 的详细图形,您当然可以构建一个简单的路径,该路径将(实际上)覆盖以进行命中测试.

The easiest approach (from a hit-testing standpoint) is to do what @PhillipMills suggested, using Quartz drawing and CGPathContainsPoint(). If you have detailed graphics that you need rendered as a PNG, you could certainly construct a simple path that would be (virtually) overlayed to allow hit testing.

你的其他选择,AFAIK,是用数学方式进行命中测试,但你基本上会重新实现 CGPathContainsPoint() 但没有路径,或者使用各种查看像素颜色的技巧在您的接触点进行命中测试.如果你走这条路,谷歌搜索会得到一些有用的结果,但老实说,对于像你绘制的一样简单的形状,只需使用一些 UIBezierPath 代码在代码中重新创建.

Your other options, AFAIK, are to do hit testing mathematically, but you would basically be reimplementing CGPathContainsPoint() but without a path, or to employ various tricks that look at the color of the pixels at your touch point to do hit testing. Googling will turn up some useful results if you go this route, but honestly for a shape as simple as what you've drawn, just use some UIBezierPath code to recreate in code.

这篇关于UIImageView 不同区域的 UIGesture 识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆