I'm trying to migrate all my SpriteKit code into a SwiftUI SpriteView container. My program is a mix of SwiftUI and SpriteKit on MacOS to trade crypto.
The problem I'm having is that the scrollWheel(with event: NSEvent) function inside my SKScene object isn't getting triggered when I scroll my mouse wheel.
Previously I solved this by making the SKScene the nextResponder which worked great when I was using Cocoa.
Below is what my main entry point to SwiftUI looks like.
@main
struct ManualTradingApp: App {
var exchangeManger: ExchangeManager = BinanceManager()
var chartScene: ChartScene {
let scene = ChartScene(size: CGSize(width: 600, height: 600), manager: exchangeManger)
scene.scaleMode = .resizeFill
return scene
}
var body: some Scene {
WindowGroup {
ContentView(manager: exchangeManger, chartScene: chartScene)
}
}
}
Here is the function inside of the ChartScene class I made that is supposed to receive the scroll wheel events.
override func scrollWheel(with event: NSEvent) {
if chartNode.contains(event.locationInWindow) {
chartNode.scrollWheel(with: event)
} else if chartNode2.contains(event.locationInWindow) {
chartNode2.scrollWheel(with: event)
}
if symbolRowContainer.contains(event.locationInWindow) {
moveSymbolContainer(deltaY: -event.scrollingDeltaY * 10.0)
}
}
you can capture scrollWheel events via an NSView, then post them as notifications, which can be received by the SKScene.
first add a notification observer to your SKScene
second, intercept events in NSView and post a notification
and finally, add the NSViewRepresentable to SwiftUI