Adding SKNodes into a ScrollView?

49 views Asked by At

On Android, I've previously used CCScrollView (part of Cocos2D-X) to add nodes to a scrollable container, with all the extras like the elastic bounce, scrollbars, swipe gestures, etc.

But I can't work out how to do this in Xcode. I know there's UIScrollView, but I'm writing for macOS. And there's SKCameraNode, but this seems to target the whole scene, rather than just part of the scene, I think.

I'd like to create a horizontal scrollable container for interactive sprite nodes. I know I could simply make the node move in relation to the mouse's x value, but that's not as pleasing as a simple scrollview.

I guess I could place a second scene within the scrollview, but that seems overkill and a performance hit. There's also third party solutions like SwiftySKScrollView but I'd like to avoid relying on such dependencies.

override func mouseDown(with event: NSEvent) {
    scroller.removeAllActions()
    scrollerPosX = scroller.position.x - event.location(in: self).x
}

override func mouseDragged(with event: NSEvent) {
    mouseMomentum = event.deltaX
    for node in nodes(at: event.location(in: self)) {
        if node.isMember(of: Scroller.self) {
            scroller.position.x = event.location(in: self).x + scrollerPosX
        }
    }
}

// This needs work to improve the ease out momentum, and a boundary bounce needs adding
override func mouseUp(with event: NSEvent) {
    let moveby = SKAction.moveBy(x: mouseMomentum*10, y: 0, duration: 0.3)
    moveby.timingMode = .easeOut
    scroller.run(moveby)
}
0

There are 0 answers