Currently, I am able to transition from a UIViewController to another UIViewController.
I use the following command:
PlaygroundPage.current.liveView = SecondViewController()
but if I use the same command to transition to a UIViewController with a SKView as the default view, I get the following error:
Cannot cast UIView as! SKView
What are the steps to resolve this issue?
Example: To transition from a UIViewController to another one, I use this:
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
import AVFoundation
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let label = UILabel()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.text = "Hello World!"
label.textColor = .black
view.addSubview(label)
self.view = view
}
}
public class IntroViewController: UIViewController {
public let descriptionTextView: UITextView = {
let textView = UITextView()
textView.font = UIFont.boldSystemFont(ofSize: 18)
textView.textColor = .black
textView.translatesAutoresizingMaskIntoConstraints = false
textView.textAlignment = .center
textView.isEditable = false
textView.isScrollEnabled = false
textView.text = ""
return textView
}()
public let bb: UIButton = {
let textView = UIButton()
textView.backgroundColor = .blue
textView.setTitle("Accept the challenge", for: .normal)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.addTarget(self, action: #selector(click), for: .touchUpInside)
return textView
}()
@objc func click() {
PlaygroundPage.current.liveView = MyViewController()
}
public override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(descriptionTextView)
descriptionTextView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
descriptionTextView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
descriptionTextView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
descriptionTextView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
// Do any additional setup after loading the view.
view.addSubview(bb)
bb.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
bb.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
bb.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
// descriptionTextView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
bb.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
}
public override func viewDidAppear(_ animated: Bool) {
let str = "Cheesy MI Line!"
for i in str {
AudioServicesPlaySystemSound(1306)
descriptionTextView.text! += "\(i)"
RunLoop.current.run(until: Date()+0.12)
}
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = IntroViewController()
And to transition from UIViewController (UIView) to another UIViewController (SKView):
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
let scene = GameScene(size: self.view.bounds.size)
scene.scaleMode = .aspectFill
view.presentScene(scene)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}
override var prefersStatusBarHidden: Bool {
return true
}
}
public class IntroViewController: UIViewController {
public let descriptionTextView: UITextView = {
let textView = UITextView()
textView.font = UIFont.boldSystemFont(ofSize: 18)
textView.textColor = .black
textView.translatesAutoresizingMaskIntoConstraints = false
textView.textAlignment = .center
textView.isEditable = false
textView.isScrollEnabled = false
textView.text = ""
return textView
}()
public let bb: UIButton = {
let textView = UIButton()
textView.backgroundColor = .blue
textView.setTitle("Accept the challenge", for: .normal)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.addTarget(self, action: #selector(click), for: .touchUpInside)
return textView
}()
@objc func click() {
PlaygroundPage.current.liveView = GameViewController()
}
public override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(descriptionTextView)
descriptionTextView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
descriptionTextView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
descriptionTextView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
descriptionTextView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
// Do any additional setup after loading the view.
view.addSubview(bb)
bb.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
bb.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
bb.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
// descriptionTextView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
bb.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
}
public override func viewDidAppear(_ animated: Bool) {
let str = "Test String!"
for i in str {
AudioServicesPlaySystemSound(1306)
descriptionTextView.text! += "\(i)"
RunLoop.current.run(until: Date()+0.12)
}
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = IntroViewController()
And I get the following error:
Could not cast value of type 'UIView' (0x7fff897d42e0) to 'SKView' (0x7fff87e20a98).