The UIView above is presented by SKScene - Why do touches always go to the scene?

I have a confusing situation. First, here is the code:

class MyView: SKView { ... public override func awakeFromNib() { super.awakeFromNib() let gameScene = MyGameScene(...) // ... presentScene(gameScene) let view = UIView() view.frame = bounds view.isUserInteractionEnabled = true addSubview(view) } ... } class MyGameScene: SKScene { ... public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { print("touch received") } ... } 

I have a SKView representing a SKScene . Introducing it, I add a UIView that fills the screen. However, when I launch the application and click on the screen, “touch received” is printed, which means that the touch that should be behind the view hit the scene. Why is this?

And what can I do to prevent this?

I also tried adding a gesture recognizer to the view. This does not interfere with this problem (although I set cancelsTouchesInView to true)

Thanks for any help.

+5
source share
2 answers

It seems you are trying to achieve a kind of HUD. SceneKit is not pure UIKit, so you will need to adapt your approach - some ideas:

0
source

Your problem will be fixed if you add the view to the SKView supervisor (add it to the view controller).

So, you can try to do something in this direction:

 let view = UIView() view.frame = bounds view.isUserInteractionEnabled = true superview?.addSubview(view) 

Alternatively, you can add it to the window, but this is not recommended.

0
source

Source: https://habr.com/ru/post/1272406/


All Articles