I was trying to solve this problem (TL;DR An overlaid SKScene using the overlaySKScene property in SCNView wasn't causing a redraw when children were added and removed from it) using view.setNeedsDisplay() to force a redraw since the SCNView wasn't doing it automatically.
The problem with using view.setNeedsDisplay() was that the CPU usage was spiking to 50% and I assumed it was because the entire SCNView was having to redraw its contents, which included a 3D SCNScene as well. My solution was to use view.setNeedsDisplay(_: CGRect) to minimise the region that needs to be redrawn. However, to my surprise, no matter what I put as the CGRect value the SCNView refused to render the SKScene contents that had been overlaid on it.
Steps to reproduce issue
Open SceneKit template
From the Main (Base) storyboard, set the "Scene" attribute on the SCNView to be "art.scnassets/ship.scn" or whatever the path is
Delete all boilerplate code and just leave
class CustomSKScene: SKScene {
override func didMove(to view: SKView) {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(userTapped(_:)))
view.addGestureRecognizer(tapGestureRecognizer)
}
#objc func userTapped(_ sender: UITapGestureRecognizer) {
let finger = convertPoint(fromView: sender.location(in: view))
let circle = SKShapeNode(circleOfRadius: 25)
circle.position = finger
addChild(circle)
}
}
class GameViewController: UIViewController {
private var gameView: SCNView { view as! SCNView }
override func viewDidLoad() {
gameView.overlaySKScene = CustomSKScene(size: gameView.bounds.size)
}
}
(This should still allow the ship scene to render when you run the app)
When you tap the screen, circles shouldn't show up. Fix this issue by adding view!.setNeedsDisplay() below the addChild function. Notice how CPU usage goes up to around 40-50% if you tap repeatedly after adding this fix.
Replace view!.setNeedsDisplay() with view!.setNeedsDisplay(view!.frame) (which should be equivalent).
At this point we are now back to square one. The circles are not showing up on screen again and confusion ensues. view.setNeedsDisplay() and view.setNeedsDisplay(view.frame) should be equivalent, yet, nothing is redrawn.
Does anyone know how to fix this problem? I feel it only happens when using the overlaySKScene property so maybe there is some caveat with its implementation that I am unaware of.
Some observations:
When you debug the view hierarchy, the overlaid SKScene doesn't show up anywhere, which is strange
sender.view === view returns true
(sender.view as! SCNScene).overlaySKScene === self also returns true
Related
I am experimenting with UIKit and SceneKit for the first time and saw that there is a overlaySKScene property in my SCNView. I figured this is ideal for displaying 2D pause menus and/or overlaying joysticks to control characters. I tried a super simple implementation of overlaying a joystick when the user touches the screen like so:
class Overlay: SKScene {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
let joystick = SKShapeNode(circleOfRadius: 30)
joystick.fillColor = .red
joystick.position = touches.first!.location(in: self)
addChild(joystick)
}
}
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scnView = view as! SCNView
scnView.overlaySKScene = Overlay(size: scnView.bounds.size)
}
}
In my interface builder, GameViewController is set to be the "initial view controller" and contains a SCNView which holds my SCNScene that I created using the Xcode Scene builder (It's just a floating box and a floor). The above code, surprisingly, didn't work when I ran it. It only displayed the SCNScene (the floating box) and didn't create the circles when I touched the screen even though touchesBegan(_:with:) is executed. What's even stranger is when I press "debug view hierarchy", the circles are displayed, and when I press the "resume execution", the circles suddenly appear on the screen as well. Edit: The circles also appear when I press the home button on my iPhone and then tap the app again.
When I try to add children to the scene inside the init(size:), or sceneDidLoad() functions, the circles appear as expected. This also works if I configure the Overlay scene before using overlaySKScene. It only seems to not work when I use touchesBegan(_:with:). I am extremely confused as to why this isn't working as I assume adding children in the touchesBegan method is a very common thing to do. Is it a bug or am I doing something wrong?
Edit: I got it working by using view!.setNeedsDisplay() to force a redraw at the end of the touchesBegan method, however, now I'm confused as to why the view was not getting redrawn
I am trying to programmatically create a camera for my MainScene.scn file.
I need to create the camera in code, as I am wanting to make a camera orbit node, and this is the only way I can think of. I also would like to keep using my scene file.
This is my code (simplified) from my view controller:
import UIKit
import SceneKit
class GameViewController: UIViewController {
// MARK: Scene objects
private var gameView: SCNView!
private var gameScene: SCNScene!
private var gameCameraNode: SCNNode!
// MARK: View controller overrides
override func viewDidLoad() {
super.viewDidLoad()
// Setup game
initView()
initScene()
initCamera()
}
}
private extension GameViewController {
// Initialise the view and scene
private func initView() {
self.view = SCNView(frame: view.bounds) // Create an SCNView to play the game within
gameView = self.view as? SCNView // Assign the view
gameView.showsStatistics = true // Show game statistics
gameView.autoenablesDefaultLighting = true // Allow default lighting
gameView.antialiasingMode = .multisampling2X // Use anti-aliasing for a smoother look
}
private func initScene() {
gameScene = SCNScene(named: "art.scnassets/MainScene.scn")! // Assign the scene
gameView.scene = gameScene // Set the game view's scene
gameView.isPlaying = true // The scene is playing (not paused)
}
private func initCamera() {
gameCameraNode = SCNNode()
gameCameraNode.camera = SCNCamera()
gameCameraNode.position = SCNVector3(0, 3.5, 27)
gameCameraNode.eulerAngles = SCNVector3(-2, 0, 0)
gameScene.rootNode.addChildNode(gameCameraNode)
gameView.pointOfView = gameCameraNode
}
}
This code can easily be pasted to replace the default view controller code. All you need to do is add the MainScene.scn and drag in something like a box.
If you try the code, the camera is in the wrong position. If I use the same properties for the camera in the scene, it works, but that is not what I am looking for.
From what I have read, SceneKit may be creating a default camera as said here and here. However, I am setting the pointOfView property just as they said in those answers, but it still does not work.
How can I place my camera in the correct position in the scene programmatically?
After a while, I discovered that you can actually add empty nodes directly within the Scene Builder. I originally only wanted a programmatic answer, as I wanted to make a camera orbit node like the questions I linked to. Now I can add an empty node, I can make the child of the orbit the camera.
This requires no code, unless you want to access the nodes (e.g. changing position or rotation):
gameCameraNode = gameView.pointOfView // Use camera object from scene
gameCameraOrbitNode = gameCameraNode.parent // Use camera orbit object from scene
Here are the steps to create an orbit node:
1) Drag it in from the Objects Library:
2) Setup up your Scene Graph like so:
I'm a newbie to the iOS development and I'm trying to figure out how CATiledLayer works. NOT how to use it!
There are two key features of CATiledLayer I want to know how it works.
Visible bounds automatic reporting. How does CATiledLayer know its visible bounds have changed? Can I create a custom CALayer and register to know the same information?
Tile drawing. How are these tiles drawn? Are they drawn as sublayers?
The reason I want to find out how these two key points work is that I'm building a custom UIView that can have a very large width and a CALayer will crash due to having such a large backed layer. CATiledLayer has a light memory usage and its' almost perfect!
The reason I don't want to use CATiledLayer is how it works internally. All drawing happens on a background thread. I know you can even have the drawing logic be called on the main thread but this would happen on the next drawing cycle. Because drawing doesn't happen on the same draw cycle, during a view resize our drawing logic has a delay updating the drawings thus causing the UIView content to shake during updates by the user.
Just to add a little more to this. I'm building an audio editor where it shows the audio waveform and the user can resize this clip. The clip is shown inside a collection view. The mentioned issue above with CATiledLayer seems to be the same issue Garage band has. I can barely notice it when resizing an audio clip to the left. They are likely using CATiledLayer.
I know one way to do question number one but I am not sure of the consequences and efficiency. This is more theoretical except it works. I am using a CADisplayLink to run a check to see if the frame of the layer is in the main window. I did notice a small bit of CPU (1% or less) being used so I would test it more compared to the CATiledLayer. CATiledLayer just breaks the drawing up but operates on the same premise that only what is visible can be drawn. drawRect I think fundamentally works when visible or the visible bounds change. As far as subclass I tested I used it inside a UICollectionView and know that it works. I could even get logs of when a cell was created and not on screen. Here is the working subclass of CALayer. I don't know if this helps you but it is possible.
import UIKit
protocol OnScreenLayerDelegate:class {
func layerIsOnScreen(currentScreenSpace:CGRect)
func layerIsNotOnScreen(currentScreenSpace:CGRect)
}
class OnScreenLayer: CALayer {
var displayLink : CADisplayLink?
weak var onScreenDelegate : OnScreenLayerDelegate?
override init() {
super.init()
commonSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonSetup()
}
func commonSetup(){
displayLink = CADisplayLink(target: self, selector: #selector(checkOnScreen))
displayLink?.add(to: .main, forMode: .commonModes)
}
#objc func checkOnScreen(){
if let window = UIApplication.shared.keyWindow{
let currentRect = self.convert(self.bounds, to: window.layer)
if window.bounds.intersects(currentRect){
onScreenDelegate?.layerIsOnScreen(currentScreenSpace: currentRect)
}else{
onScreenDelegate?.layerIsNotOnScreen(currentScreenSpace: currentRect)
}
}
}
}
As to question 2 I think CATiledLayers probably does not use sublayers and instead slowly draws all the contents into a single contents image but by tiling and probably easier math for them. It might be something that takes the visible area draws it in the background and provides the layer contents. Then caches that section and adds another until it is complete.This is only a guess.
Here is the code from my test in a collectionView cell.
import UIKit
class AlbumCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var imageView: UIImageView!
var onScreenLayer = OnScreenLayer()
var currentIndex : Int = 0
override func awakeFromNib() {
super.awakeFromNib()
onScreenLayer.frame = self.bounds
self.layer.addSublayer(onScreenLayer)
onScreenLayer.onScreenDelegate = self
}
override func layoutSubviews() {
super.layoutSubviews()
onScreenLayer.frame = self.bounds
}
}
extension AlbumCollectionViewCell : OnScreenLayerDelegate{
func layerIsOnScreen(currentScreenSpace: CGRect) {
//or more proof
print("it is on screen \(currentIndex)")
}
func layerIsNotOnScreen(currentScreenSpace: CGRect) {
print("not on screen \(currentIndex)")
}
}
The current index was set in cellForItem so I could monitor it. Let me know if this helps. Also the check could modify the frame to catch it right before it comes on screen by a margin that way you are drawing prior to that.
I am trying to transition from the default, root scene to a new scene with SpriteKit. However, whenever I press the Start button, it grays out the old scene (although it remains visible) and the Drawing Board label shows up. The scene remains greyed out. All the buttons from the old scene can still be pressed but do not perform their associated actions. A UIButton triggers this func:
startButton.addTarget(self, action: "goToDrawingBoard:", forControlEvents: UIControlEvents.TouchUpInside)
The func:
#objc func goToDrawingBoard(sender: UIButton){
let drawingBoardScene = DrawingBoardScene(size: self.size)
self.scene?.view?.presentScene(drawingBoardScene, transition: SKTransition.crossFadeWithDuration(1.0))
}
And the DrawingBoardScene.swift file:
import Foundation
import SpriteKit
import UIKit
class DrawingBoardScene: SKScene {
let titleLabel = SKLabelNode(text: "DRAWING BOARD")
override func didMoveToView(view: SKView) {
/*LABEL: Displays title*/
titleLabel.fontColor = UIColor.blackColor()
titleLabel.fontSize = 60
titleLabel.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
self.addChild(titleLabel)
}
}
Looks like you are presenting the scene incorrectly, try the following:
#objc func goToDrawingBoard(sender: UIButton){
let drawingBoardScene = DrawingBoardScene(size: self.size)
self.view?.presentScene(drawingBoardScene, transition: SKTransition.crossFadeWithDuration(1.0))
}
There is no reason to add the new scene as a child to the old scene, and who knows why your scene has a scene object.
As a personal note, presenting your scene in this matter is not a good way to present scenes. It is the views job to be presenting scenes, so what you should be doing is when it comes time for the scene to be removes, send a notification in some way to the view that the scene is done working and is waiting for it to be removed, and have the view then present the scene. This will allow the view to properly remove the old scene without having any retainers holding it back. One method to do this is threw delegation
My friend and I spent a couple nights working on various solutions and the one we finally came up with is this:
override func willMoveFromView(view: SKView) {
self.removeAllChildren()
delete(startButton)
}
override func delete(sender: AnyObject?) {
let subviews = (self.view?.subviews)! as [UIView]
for v in subviews {
if let button = v as? UIButton {
button.removeFromSuperview()
}
}
}
The only problem with this is that when the scene shifts the buttons can take a split second longer to disappear, giving it a kind of glitchy feel. It does work though, so for a short term solution it is great.
As the user taps on superview I am detecting it via UITapGestureRecognizer and calling below function to create a UIImage view, add it to superview, add gravity to it and let it fall out of the superview.
Problem is that if the user taps again while the first UIImage is still on the screen, first image halts in place and the second UIImage starts falling. What is the best way to let multiple UIImage fall independently on the screen.
Secondly, I feel, I should be removing these UIImage from superview to conserve memory (as there could be hundreds of taps but I cant figure out how to automatically remove a UIImage as soon as it is out of the screen.
Help in swift please.
func dropBall(tapLocation:CGPoint) {
let ball = UIImageView(image: UIImage(named: "White50.png"))
ball.frame = CGRect(origin: CGPoint(x:tapLocation.x-25,y:tapLocation.y-25), size: CGSize(width:50,height:50))
view.addSubview(ball)
animator = UIDynamicAnimator(referenceView: self.view)
gravity = UIGravityBehavior(items: [ball])
animator.addBehavior(gravity)
}
The reason the first animation stops is that when you call this again, you are instantiating a new animator and releasing the old one. You should instantiate the animator only once. Likewise, you should instantiate gravity just once.
In terms of stopping the behavior once the view is off screen, one technique is to add an action block for the behavior, iterate through the behavior's items and see if each is still on screen, and remove the item from the behavior's list of items when the item falls off screen.
override func viewDidLoad() {
super.viewDidLoad()
animator = UIDynamicAnimator(referenceView: view)
gravity = UIGravityBehavior()
gravity.action = { [unowned self] in
let itemsToRemove = self.gravity.items.filter() { !CGRectIntersectsRect(self.view.bounds, $0.frame) }
for item in itemsToRemove {
self.gravity.removeItem(item as UIDynamicItem)
item.removeFromSuperview()
}
}
animator.addBehavior(gravity)
}
Now, to give an item gravity, just add the item to the gravity behavior and it will automatically be removed when it falls out of view:
gravity.addItem(ball)