'True 2-dimensional scrolling in Swift/SwiftUI

I'm trying to build a simple macOS app that allows true 2-dimensional scrolling through a canvas-like space.

ScrollView([.horizontal, .vertical]) { ... } doesn't cut it; by itself, it only allows either horizontal or vertical scrolling at any given time. You can't scroll smoothly in any other direction (e.g. at 45º to the horizontal); it seems to lock you to the nearest cardinal axis.

What's the easiest way to achieve smooth omnidirectional scrolling in Swift/SwiftUI?

(Preferably in a way that is resource-smart and doesn't attempt to render objects/views that are too far offscreen; I expect to have a lot of things going on, and don't want to be rendering unnecessarily.)


If you want to replicate this, here's my code—you can start a new XCode project > macOS > App, then in ContentView.swift replace struct ContentView: View { ... } with the following.

struct ContentView: View {
    var body: some View {
        ScrollView([.horizontal, .vertical]) {
            CircleView()
        }
    }
}

struct CircleView: View {
    var body: some View {
        Path { p in p.addEllipse(in:
                    CGRect(x: 250, y: 250, width: 500, height: 500)
                    )
        }.frame(width: 1000, height: 1000)
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source