0

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)
}
Mateus
  • 357
  • 1
  • 4
  • 14
  • 1
    You're more likely to attract people to answer your question if you refocused it on your concrete problem, and remove the ranty editorial part. Scroll views and Scene kit scenes are essentially totally different systems. You _can_ put a scene in a scroll view, but you shouldn't. That's just not how it's meant to be used. – Alexander Jan 24 '23 at 18:35
  • @Alexander point taken! I've edited the post. :) – Mateus Jan 24 '23 at 21:54
  • 1
    I still find the question a bit unclear, and I suspect others might to. Your question's title is formulated as an [XY problem](https://xyproblem.info/). You're asking for help about how to put `SKNode`s into an `UIScrollView` (problem Y), because you're assuming that solving problem Y will fix your underlying problem X. What if the correct solution to problem X has absolutely nothing to do with scrolls views? In other words, forget about scroll views. Explain to us what your true, underlying goal is, and readers will give their input on the best approach. – Alexander Jan 24 '23 at 22:12
  • My goal is to create a container (an SKNode) that has many interactive sprite children (SKSpriteNodes). The container must be horizontally scrollable (e.g. it's 3 x the width of the screen, but only about 1/4 high). Currently I'm using the mouse drag position and event.deltaX values to scroll the container and add momentum. I am assuming there's a better way to do achieve this. – Mateus Jan 24 '23 at 23:35
  • 1
    What's the difference between what you're trying to achieve, and what a moving camera node would achieve? – Alexander Jan 24 '23 at 23:50
  • 1
    as @Alexander mentioned these are two different systems. with SpriteKit your two basic strategies are (1) move the camera, or (2) put everything in a base "container" node and move that. i would suggest the former. down side is you don't get for free all the nice usability/chrome that something like UIScrollView provides (such as scrollbars, nice springy momentum movement, etc). you have to code that yourself. – Fault Jan 25 '23 at 00:09
  • Thanks both, you've answered my query. I'll just code a scrolling view as suggested. I guess I just felt SpriteKit must have something already built in, and I didn't want to reinvent the wheel. – Mateus Jan 25 '23 at 00:26
  • 1
    "I just felt SpriteKit must have something already built in, and I didn't want to reinvent the wheel." Interestingly enough, no where in your question do you mention this :p It was so fixated on the particular approach of embedded SKNodes into UIScrollViews – Alexander Jan 25 '23 at 14:50

0 Answers0