layer hit test only returning layer when bottom half of layer is touched

后端 未结 8 1975
闹比i
闹比i 2021-02-10 19:14

I have a sublayer on a layer-backed view. The sublayer\'s contents are set to an Image Ref and is a 25x25 rect.
I perform a hit test on the super layer when the touchesBega

相关标签:
8条回答
  • 2021-02-10 19:17

    If touches are being recognized on the lower part only, one possibility is that another subview is covering the top half of this subview. Do you have multiple subviews or only one image? In case you have multiple subviews and any of them have the background color as clearColor, try giving a solid background color to it for testing. That ways you'll know if your subview is getting covered by another subview or not.

    Hope that helps.

    0 讨论(0)
  • 2021-02-10 19:19

    /// Add frame.origin.y

        public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
            let convertPoint = CGPoint(x:point.x, y:(point.y + frame.origin.y))
            let superPiont = self.layer.convert(convertPoint, to:layer.superlayer)
            selectlayer = layer.superlayer?.hitTest(superPiont)
            }
    
    0 讨论(0)
  • 2021-02-10 19:28

    I have simplified my code to find the problem. I only have one sublayer on screen and no subviews. The code listed above is what I am running in the simulator. Since there are only 2 layers on screen, the backing layer of the host view and the sublayer I have added, nothing should interfere with the hit test.

    In case it wasn't clear, when the top portion of the sublayer is touched, the hit test return the backing layer instead. Also if I touch the backing layer at a point just below the sublayer, the sublayer is returned by the hit test.

    It is hard to explain but if you run the code, it becomes clear.

    Thanks

    0 讨论(0)
  • 2021-02-10 19:30

    The accepted answer does not solve the problem, but the answer by schwa does.

    I have a window, a view controller and view. The view receives touches and calls hitTest: on its layer. I had the same problem and found out that the point in the view is correct, but in hitTest: the y coordinate of the point is 20px off, which happens to be the height of the status bar.

    By mapping the point to the coordinate system of the superlayer as suggested by schwa this problem is fixed. The "mysterious" superlayer is the root layer of the superview. In my case, it is the window layer (frame (0, 0, 320, 480)).

    0 讨论(0)
  • 2021-02-10 19:35

    Thanks Kevin...

    I ended up just redoing my code with UIViews. It was too much trouble just trying to figure out why the UIView didn't know what layer was being touched. My whole reasoning behind the idea was that I will have a 100+ items on screen at one time, and thought Layers would be easier on the processor than UIViews. I have the app up and running now and 100 views isn't giving it any hiccups.

    My suggestion to others is just stick with hit testing UIViews when possible, it makes the code much simpler

    0 讨论(0)
  • 2021-02-10 19:40

    When I try your code, I get almost-correct behavior, except touches are often not recognized on the very edge of the sublayer. I am a bit confused as to what's going on. You might want to try doing something like when you register a touch, throw a new tiny layer up where the touch occurred (like a 5x5 square of some color) and then remove it again 3 seconds later. That way you can track where CA thinks the touch occurred versus where your cursor actually is.

    BTW, you don't need to create a CGImage for your blue content, you can just set the layer's backgroundColor to [UIColor blueColor].CGColor.

    0 讨论(0)
提交回复
热议问题