如何检测触摸点是否被CALayer的绘制内容包含?
UIView 中添加了三层。一层绘制一个矩形。一个人画一个圆圈。一个画一个多边形。该图层的不透明度为否。当我触摸多边形时,我想获得绘制多边形的正确图层。这三层都充满了视野。我已经实现了这个。但我不知道我们是否有更好的解决方案来解决这个问题。我的方法是这样的: 1.使用-drawLayer:inContext绘制内容。存储您使用的 CGPath。 2.在UIView的-touchedEnded:withEvent方法中。使用 CGPathContainsPoint() 检测触摸点是否包含在 CGPath 中。
也许这是解决这个问题的愚蠢方法。谁能告诉我如何更好地解决这个问题?
There are three layers added to UIView. One layer draws a rectangle. One draws a circle. One draws a polygon. The layer's opacity is no. When I touched the polygon, I want to get the correct layer which draws the polygon. And the three layers are full filled to the view. I have implemented this. But I don't know if we have better solution to solve it .My way is like this:
1.Drawing the content using -drawLayer:inContext. store the CGPath that you used.
2.In the UIView's -touchedEnded:withEvent method. using CGPathContainsPoint() to detect if the touch point is contained by the CGPath.
Maybe this is the stupid way to solve this. Anyone who can tell me how to solve it better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要对路径进行准确的命中测试,如果该点位于您的路径内部,那么您必须按照您的建议使用 CGPathContainsPoint 自行检查/迭代图层层次结构。
在迭代时,您可以通过跳过点位于框架之外的层来优化它。
对于不太细粒度的控制,您可以使用 CALayer
方法获取触摸层。
如果您有一个嵌套级别 < 的图层层次结构1000(这几乎总是正确的)我不会太担心。
If you need an accurate hit test for path's I'm afraid you have to check/iterate the layer hierarchy yourself if the point is inside your path using
CGPathContainsPoint
as you suggested.While iterating you could optimize it by skipping layers where the point is outside their frame.
For less fine grained control you can get the touched layer by using
CALayer
smethod.
If you have a layer hierarchy with a nesting level < 1000 (which is almost always true) I would not worry too much.