How to present a view on top of SKScene? - swift

I know there is many question out there on the same topic but still I didn't get any idea or solution to make it work. How do I implement a small view on top of a scene with options as shown in image?

You can do it within your scene using spritekit nodes or you can create a UIView from your viewController and add it the viewControllers view:
something like this:
// set up base view of the game
self.baseView = UIView(frame: self.view.frame)
self.view.addSubview(self.baseView)
// add my skView to my baseView
self.baseView.addSubview(skView)
// add pause button on top or other elements
// hide or show these as needed
self.baseView.addSubview(self.pauseBtn)
self.addJoySticks()

You could just use SpriteKit itself and:
scene?.view?.presentScene(anotherScene, transition: SKTransition.fadeWithDuration(1.0))
edit Just to add, If any Background AVPlayers or nodes are in play, then they're all discarded when transitioning.

Related

Override hitTest SwiftUi?

I have a transparent view on top of another view in a z stack and would like to interact with them both simultaneously. How can I override hit test in swiftui so I can interact with both views in the z stack on top of each other?
I found a way using ui kit but how can I do the same swiftui? How can I click a button behind a transparent UIView?
I tried changing both views zindex to 0 but doesn’t work as one view still cancels the other out. Any thoughts?
// I want both views to be interctable but shows that z layering
ZStack {
TestView()
MainView().allowsHitTesting(true) // I still want this view to be interactable
}

Add a Sprite Kit View to a UIView Xcode 8.2.1

I am making a app that will hopefully have a game in it but in order to do that I need a Sprite Kit View. I was wondering if there was a way to add a Sprite Kit View to a Single View Application?
Yes, SKView is just a subclass of UIView, so you can add it via code where you want or through the Storyboard. as of XCode 9 I believe, IB allows you to add an actual SKView where you can set the scene file instead of having to fill the custom class field in.

Swift OSX dynamically add items to a scroll list

In an app I created, I drag and dropped a 'Scroll View' to the main storyboard. This automatically creates a NSScrollView, inside of which is an NSClipView, inside of which is an NSView. Fine.
What I would like to achieve is to dynamically add checkboxes to that scroll view. I don't know how many checkboxes there will be in advance (which is why I need a scroll).
adding a subView to either the NSScrollView or the NSClipView actually displays some checkboxes, but the scroll is not enabled. Adding a subView to the NSView renders nothing.
Edit: I have also tried to modify the NSScrollView, NSClipView, and NSView's height, with no luck.
How can I enable the scrolling when too many checkboxes are added to the view?
So the trick to this is creating a container view (i.e. NSView) to hold the buttons. When you create a NSScrollView in the storyboard, it creates NSClipView and NSView for you. After you add or remove a button to the NSView (you can create your own, or use the one provided), set the documentView of the scroller to the view that contains your buttons. That resets everything properly in the scroller, and you should be scrolling! If you change the size of the view that represents the scrolling content, you just reassign that view to scroller.documentView and it will update everything accordingly.
Here is a horrible example of adding 20 buttons to a scroller that will scroll:
let documentView = NSView(frame: CGRectMake(0,0,200,1200))
for index in 0..<20 {
let offset = CGFloat(index * 50)
let button = NSButton(frame: CGRectMake(0, offset, 150, 50 ))
documentView.addSubview(button)
}
scrollView.documentView = documentView
The Apple documentation on this gives more detail and further options you may want to set.
Hope this helps!

SWIFT - Can I have a SKView as a subview of a UIView?

In my storyboard, I have a base UIView, then a UIView containing a square board drawing in a CGRect and buttons and status fields above and below. I tried drawing the playing pieces but animating them became a nightmare so I have rendered them as sprite nodes in an array and they animate themselves. So far so good. Then layer on top of that is a SKView view and I want to present the SpriteKit scene in there with skView.allowsTransparency = true so I just have the sprites over a transparent background (revealing the board below) and using TapGestureRecognizers to effect the sprite animation event handling.
So in IB hierarchy is: VC / UIView / boardView: UIView / skView
Problem is, the Spritekit scene always targets the top level UIView not skView so game pieces are behind the board. Am I missing something simple here?
I'm gonna try placing a child VC where skView is but any assist, if I've missed a trick, would be very much appreciated.
I wish I could see some code, but what you do is create an IBOutlet for your SKView, then have that outlet present the scene, I am currently doing this in an app I am working on, so I know it is doable

How to place sprite kit elements overtop of regular UIKit elements

I'm trying to geive the user the ability to drag a skspritenode over a UIImageView and UIButton, but whenever I create the scene it appears behind the UI elements. How do I fix this?
I am afraid that is just not possible. All the sprites and any other elements are rendered frame by frame on the SKScene node and cannot be "dragged" or placed anywhere else. These are not UIKit elements.
In this case, your view is acting as the SKScene and hence all the sprites are being rendered onto that.
SKView is a UIView/NSView in itself. All the Sprite Kit nodes (the scene is also a node) are constituents of the SKView and contained within it. You can only change the draw order of views but not their contents.
To give an example: what you're trying to do is the equivalent of trying to place a UIImageView on top of a UIButton's background but below the UIButton's label.
While technically you could achieve this for plain UIView elements with some trickery, the same will never work for Sprite Kit nodes. They aren't views to begin with but drawn onto an OpenGL framebuffer represented/managed by the SKView. From the perspective of Cocoa an SKView is a single view with no subviews in it.
Simply put: nodes are not views, and nodes are technically incompatible to views.
The UIImageView and its UIButton and whatever other content you want in there must be in a UIView that's sitting lower than the SKView you have the Sprite Kit contents in.
And you'll need to ensure the SKView is transparent (at least its background) and that it passes touches through to the UIView underneath if you need them down there sometimes.