简体   繁体   中英

How to detect if the touch point is contained by draw content of CALayer?

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?

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 s

- (CALayer *)hitTest:(CGPoint)thePoint

method.

If you have a layer hierarchy with a nesting level < 1000 (which is almost always true) I would not worry too much.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM