SpriteKit - Pause screen doesn't show when didBecomeActive - sprite-kit

My pause system works perfectly from inside the game, and also when the app moves to the background and then becomes active again the game stays paused, but my problem now is when it becomes active my pause screen doesn't show.
AppDelegate:
func applicationDidBecomeActive(application: UIApplication) {
NSNotificationCenter.defaultCenter().postNotificationName("Pause", object: nil)
}
ViewController:
override func viewDidLoad() {
super.viewDidLoad()
let scene = GameScene()
// Configure the view.
let skView = self.view as! MainView
NSNotificationCenter.defaultCenter().addObserver(skView, selector: "setStayPaused", name: "Pause", object: nil)
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
scene.size = skView.bounds.size
skView.presentScene(scene)
}
MainView (My custom skView):
class MainView: SKView {
var stayPaused = false as Bool
override var paused: Bool {
get {
return super.paused
}
set {
if (!stayPaused) {
super.paused = newValue
}
stayPaused = false
}
}
func setStayPaused() {
if (super.paused) {
self.stayPaused = true
}
}
}
GameScene:
override func didMoveToView(view: SKView) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "pauseGame", name: "Pause", object: nil)
}
func pauseGame() {
if isFirstTime == false { // to make sure that the app did not just get launched
pauseScreen.hidden = false // doesn't show
pauseButton.hidden = false // doesn't show
view?.paused = true
scene?.paused = true
}
}

Setting the pause screen's and button's hidden property has no effect because the view and/or scene is paused. You will need to temporarily un-pause the view, set the hidden properties to false, return the main loop, and then re-pause the view. Here's an example of how to do that.
AppDelegate:
func applicationWillResignActive(application: UIApplication) {
NSNotificationCenter.defaultCenter().postNotificationName("PauseViewNotification", object:nil)
}
func applicationDidBecomeActive(application: UIApplication) {
NSNotificationCenter.defaultCenter().postNotificationName("ShowPauseScreenNotification", object:nil)
}
MainVew (SKView subclass):
class MainView: SKView {
override var paused: Bool {
get {
return super.paused
}
set {
}
}
func pause() {
super.paused = true
}
func resume() {
super.paused = false
}
func togglePause() {
super.paused = !super.paused
}
}
ViewController:
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene(fileNamed:"GameScene") {
// Configure the view.
let skView = self.view as! MainView
NSNotificationCenter.defaultCenter().addObserver(skView, selector:Selector("pause"), name: "PauseViewNotification", object: nil)
}
}
GameScene:
override func didMoveToView(view: SKView) {
NSNotificationCenter.defaultCenter().addObserver(self, selector:Selector("pauseGame"), name: "ShowPauseScreenNotification", object: nil)
}
func pauseGame() {
if (!isFirstTime) {
pauseScreen.hidden = false
pauseButton.hidden = false
// Un-pause the view so the screen and button appear
if let customView = self.view as? MainView {
customView.resume()
}
// Re-pause the view after returning to the main loop
let pauseAction = SKAction.runBlock({
[weak self] in
if let customView = self?.view as? MainView {
customView.pause()
}
})
runAction(pauseAction)
}
isFirstTime = false
}
You can toggle between the pause states with
if let customView = self.view as? MyView {
customView.togglePause()
}

Related

Removing Animation From UINavigationController Pushed Unwind?

I have a simple push segue from one view controller to another. I want both segues (original and unwind to be unanimated).
In the attached playground, specifying false for the segue does, indeed remove the PUSH animation, but not the UNWIND animation.
Is there a way to remove the implicit animation in the UNWIND segue?
import UIKit
import PlaygroundSupport
class SourceViewController : UIViewController {
#objc func goDestination(_: Any) {
navigationController?.pushViewController(DestinationViewController(), animated: false)
}
override func loadView() {
view = UIView()
navigationItem.title = "SOURCE"
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(goDestination(_:)))
}
}
class DestinationViewController : UIViewController {
override func loadView() {
view = UIView()
view.backgroundColor = .yellow
navigationItem.title = "DESTINATION"
}
}
PlaygroundPage.current.liveView = UINavigationController(rootViewController: SourceViewController())
Those animations are fully customizable from the UINavigationControllerDelegate, check out the first "Supporting Custom Transition Animations" method
https://developer.apple.com/documentation/uikit/uinavigationcontrollerdelegate
Some details here https://www.youtube.com/watch?v=jWckfDNUJVY
at 23 minutes.
I'm greenchecking #glotcha's answer. It led me to this Medium post, which led me to this solution (I really wanted a fade transition):
import UIKit
import PlaygroundSupport
class TransitioningAnimatorInOut: NSObject, UIViewControllerAnimatedTransitioning {
var presenting: Bool = false
func transitionDuration(using: UIViewControllerContextTransitioning?) -> TimeInterval { 0.75 }
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromView = transitionContext.view(forKey: .from),
let toView = transitionContext.view(forKey: .to) else { return }
let container = transitionContext.containerView
if presenting {
container.addSubview(toView)
toView.alpha = 0.0
} else {
container.insertSubview(toView, belowSubview: fromView)
}
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
if self.presenting {
toView.alpha = 1.0
} else {
fromView.alpha = 0.0
}
}) { _ in
let success = !transitionContext.transitionWasCancelled
if !success {
toView.removeFromSuperview()
}
transitionContext.completeTransition(success)
}
}
init(presenting inPresenting: Bool) { presenting = inPresenting }
}
extension UINavigationController: UINavigationControllerDelegate {
public func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
if operation == .push {
return TransitioningAnimatorInOut(presenting: true)
} else {
return TransitioningAnimatorInOut(presenting: false)
}
}
}
class SourceViewController : UIViewController {
#objc func goDestination(_: Any) {
navigationController?.pushViewController(DestinationViewController(), animated: true)
}
override func loadView() {
view = UIView()
view?.backgroundColor = .yellow
navigationItem.title = "SOURCE"
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(goDestination(_:)))
}
}
class DestinationViewController : UIViewController {
override func loadView() {
view = UIView()
view?.backgroundColor = .red
navigationItem.title = "DESTINATION"
}
}
PlaygroundPage.current.liveView = UINavigationController(rootViewController: SourceViewController())

Problem with delegates removing annotation

I have two screens. The first one (firstViewController) has a mapView with a UITapGestureRecognizer. When the user taps the screen, an annotations is added to the map and the second screen (secondViewController) is presented.
When the user dismisses the secondViewController and comes back to the first one, the annotation should be removed. I know I have to use delegation, but I just can't make it to work.
This is the code I have now:
class firstViewController: UIViewController, AnnotationDelegate {
let mapView = MKMapView()
var temporaryPinArray = [MKPointAnnotation]()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(mapView
let gesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
mapView.addGestureRecognizer(gesture)
secondVC.annotationDelegate = self
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
mapView.frame = view.bounds
}
#objc func handleTap(_ gestureReconizer: UILongPressGestureRecognizer) {
let location = gestureReconizer.location(in: mapView)
let coordinates = mapView.convert(location, toCoordinateFrom: mapView)
mapView.removeAnnotations(mapView.annotations)
let pin = MKPointAnnotation()
pin.coordinate = coordinates
temporaryPinArray.removeAll()
temporaryPinArray.append(pin)
mapView.addAnnotations(temporaryPinArray)
// Present secondViewController
let secondVC = SecondViewController()
panel.set(contentViewController: secondVC)
panel.addPanel(toParent: self)
}
func didRemoveAnnotation(annotation: MKPointAnnotation) {
mapView.removeAnnotation(annotation)
}
}
Second View Controller
protocol AnnotationDelegate {
func didRemoveAnnotation(annotation: [MKPointAnnotation])
}
class SecondViewController: UIViewController {
var annotationDelegate: AnnotationDelegate!
let mainVC = firstViewController()
let closeButton: UIButton = {
let button = UIButton()
button.backgroundColor = .grey
button.layer.cornerRadius = 15
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(closeButton)
closeButton.addTarget(self, action: #selector(dismissPanel), for: .touchUpInside)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
closeButton.frame = CGRect(x: view.frame.width-50, y: 10, width: 30, height: 30)
}
#objc func dismissPanel() {
self.dismiss(animated: true, completion: nil)
annotationDelegate.didRemoveAnnotation(annotation: mainVC.temporaryPinArray)
}
}
Thank you so much for your help!
You created a new instance of firstViewController inside SecondViewController. This instance is unrelated to the actual first one:
let mainVC = firstViewController()
This means that temporaryPinArray is different as well. So instead of passing in this unrelated array...
#objc func dismissPanel() {
self.dismiss(animated: true, completion: nil)
annotationDelegate.didRemoveAnnotation(annotation: mainVC.temporaryPinArray)
}
Just change the function to take no parameters instead:
protocol AnnotationDelegate {
func didRemoveAnnotation() /// no parameters
}
#objc func dismissPanel() {
self.dismiss(animated: true, completion: nil)
annotationDelegate.didRemoveAnnotation() /// no parameters
}
And inside firstViewController's didRemoveAnnotation, reference the actual temporaryPinArray.
func didRemoveAnnotation() {
mapView.removeAnnotations(temporaryPinArray) /// the current array
}

Could not cast value of type 'Pong.MenuVC' (0x1072ea808) to 'Pong.GameViewController'

I have built an app using Xcode and swift 5.
Every time I click the "Easy", "Medium", "Hard" or "2 Player" button I get an error:
Could not cast value of type 'Pong.MenuVC' (0x1072ea808) to
'Pong.GameViewController'
Does anyone know how to fix it?
Thank you for your help.
Code of MenuVC:
enum gameType {
case easy
case medium
case hard
case player2
}
class MenuVC : UIViewController {
#IBAction func Player2(_ sender: Any) {
moveToGame(game: .player2)
}
#IBAction func Easy(_ sender: Any) {
moveToGame(game: .easy)
}
#IBAction func Medium(_ sender: Any) {
moveToGame(game: .medium)
}
#IBAction func Hard(_ sender: Any) {
moveToGame(game: .hard)
}
```
**Code with Thread 1: signal SIGABRT:**
```swift
func moveToGame(game : gameType) {
let gameVC = self.storyboard?.instantiateViewController(withIdentifier: "gameVC") as! GameViewController
currentGameType = game
self.navigationController?.pushViewController(gameVC, animated: true)
}
}
Code of GameViewController:
import UIKit
import SpriteKit
import GameplayKit
var currentGameType = gameType.medium
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
scene.size = view.bounds.size
// Present the scene
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 func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override var prefersStatusBarHidden: Bool {
return true
}
}
In your storyboard the id gameVC is of type MenuVC not GameViewController so change class name of the vc
let gameVC = self.storyboard?.instantiateViewController(withIdentifier: "gameVC") as! GameViewController

Setting Scroll View's zoomScale and hitting back button crashes program

I was trying to make a program where you click an image and it segues to show it on full screen, scaled so that there is no whitespace around (CS 193P assignment 4 task 7 for anyone who knows it). View controllers are embedded in navigation controller. It turns out that the program crashes with EXC_BAD_ACCESS (code = EXC_I386_GPFLT) when I hit the back button in the navigation bar.
I found out that I can prevent this crash by scrolling to the top of the image and then going back. Another way to completely prevent it is to comment out the line that sets scrollView's zoomScale. Here's the code I used for loading and handling the image:
import UIKit
class ImageViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView! {
didSet {
scrollView.contentSize = imageView.frame.size
scrollView.delegate = self
scrollView.minimumZoomScale = 0.03
scrollView.maximumZoomScale = 5.0
}
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
var imageURL: NSURL? {
didSet {
image = nil
if view.window != nil {
fetchImage()
}
}
}
private func fetchImage() {
if let url = imageURL {
let qos = Int(QOS_CLASS_USER_INITIATED.value)
dispatch_async(dispatch_get_global_queue(qos, 0)) {
let imageData = NSData(contentsOfURL: url)
dispatch_async(dispatch_get_main_queue()) {
if url == self.imageURL {
if imageData != nil {
self.image = UIImage(data: imageData!)
} else {
self.image = nil
}
}
}
}
}
}
private var imageView = UIImageView()
private var image: UIImage? {
get { return imageView.image }
set {
imageView.image = newValue
imageView.sizeToFit()
scrollView?.contentSize = imageView.frame.size
scrollViewDidScrollOrZoom = false
autoScale()
}
}
private var scrollViewDidScrollOrZoom = false
private func autoScale() {
if scrollViewDidScrollOrZoom {
return
}
if let sv = scrollView {
if image != nil {
sv.zoomScale = max(sv.bounds.size.width / image!.size.width, sv.bounds.size.height / image!.size.height)
sv.contentOffset = CGPoint(x: (imageView.frame.size.width - sv.frame.size.width) / 2, y: (imageView.frame.size.height - sv.frame.size.height) / 2)
scrollViewDidScrollOrZoom = false
}
}
}
func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView!, atScale scale: CGFloat) {
scrollViewDidScrollOrZoom = true
}
func scrollViewDidScroll(scrollView: UIScrollView) {
scrollViewDidScrollOrZoom = true
}
override func viewDidLoad() {
super.viewDidLoad()
scrollView.addSubview(imageView)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if image == nil {
fetchImage()
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
autoScale()
}
}
I tried to make a simple application which segues into ImageViewController from a View Controller that has only a button and is embedded in navigation controller, and the crash is still the same. Here's the code I used in the first view controller:
class FirstViewController: UIViewController {
let url = NSURL(string: "https://developer.apple.com/swift/images/swift-og.png")
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Show Full Image" {
let ivc = segue.destinationViewController as! ImageViewController
ivc.imageURL = url
}
}
}
What causes this and am I doing something wrong here? I'm using Xcode 6.3 beta 3.
It seems that the problem is actually with the scrollView's delegate, as I was able to fix this with adding
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
scrollView.delegate = nil
}

Dismissing GameCenter View in Swift

After showing a Game Center view it doesn't dismiss after tapping done. Here's my code to show it:
let gameCenterController = GKGameCenterViewController()
self.presentViewController(gameCenterController, animated:true, completion: nil)
What am I missing?
There is some good Q&A on this over at Ray Wenderlich. See the last method in the code below. Source:
http://www.raywenderlich.com/forums/viewtopic.php?f=2&t=18671
class GameViewController: UIViewController, GKGameCenterControllerDelegate {
var skView: SKView!
var scene: GameScene!
override func viewDidLoad() {
super.viewDidLoad()
// View
//------
skView = self.view as SKView
skView.ignoresSiblingOrder = true
scene = GameScene.sceneWithSize(skView.bounds.size)
scene.scaleMode = .AspectFill
scene.view?.window?.rootViewController = self
skView.presentScene(scene)
authenticateLocalPlayer()
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
var touch:UITouch = touches.anyObject() as UITouch
var location:CGPoint = touch.locationInNode(scene)
if (scene.gameCenterRect.contains(location) && GKLocalPlayer.localPlayer().authenticated) {
self.openGameCenter()
}
}
func openGameCenter() {
var gameCenter = GKGameCenterViewController()
gameCenter.gameCenterDelegate = self
self.presentViewController(gameCenter, animated: true, completion: nil)
}
func authenticateLocalPlayer(){
var localPlayer = GKLocalPlayer()
localPlayer.authenticateHandler = {(viewController, error) -> Void in
if ((viewController) != nil) {
self.presentViewController(viewController, animated: true, completion: nil)
}else{
println("(GameCenter) Player authenticated: \(GKLocalPlayer.localPlayer().authenticated)")
}
}
}
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!) {
gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
}
[...] // standard methods
}