Fade in to second window

macOS, Storyboards, Swift.


I have a button to perform a segue to a second page. I can make a control drag from the button to the second page. In Attributes, I have selected a Kind Sheet. Is it possible to open it with a fade in?


To make things easy let's say that:

The first window is ViewController.swift

The second window is SecondViewController

I have button1 in ViewController.swift with segue1 that goes to SecondViewController.swift

Accepted Reply

OK, here is the complete setup. I tested. May probably be optimized, but that's to show.


0. In the storyboard, I have 2 windowControllers, each with a viewController.

The second window controllr has an Identifier : S1W2


In the first window, I have a button that will call the second window and show with fadeIn.


1. In the VewController file for the FIRST window, define a var for the secondWindowController:


import Cocoa

class ViewController: NSViewController {

     var secondWindowController: NSWindowController? // could also be defined outside the class


2. Create a button in this VC of First window in the storyboard

No IBOutlet needed


3. Create the fadeIn function in class ViewController:


    func fadeInMyWindow() {
        print("Starts fade")     // Just to check it's called
        if self.secondWindowController == nil || self.secondWindowController!.window == nil { return }     // Better be cautious

        DispatchQueue.main.async { //   UI
            self.secondWindowController!.window!.alphaValue = 0.0
            self.secondWindowController!.window!.makeKeyAndOrderFront(nil)
        }
        let centiemeSeconde = UInt32(10_000)     // Can adjust the fade in speed
        for i in 0...99 {
            usleep(centiemeSeconde)     // I wait 1/100 second to have a visible effect
            DispatchQueue.main.async { // UI ;
                self.secondWindowController!.window!.alphaValue = CGFloat(i) / 100
            }
        }
    }


4. Connect the button to its IBAction from stroyboard to class ViewController ; connection is done by this action, bnot through a segue:


    @IBAction func ShowWindowByCode(_ sender: NSButton) {
     
        let storyboard = NSStoryboard(name: "Main", bundle: nil)
        if let windowController = storyboard.instantiateController(withIdentifier: "S1W2") as? NSWindowController
        {
            secondWindowController = windowController
            DispatchQueue.global(qos: .userInitiated).async {     // Needed for animation to occur
                self.fadeInMyWindow()
            }
        }
    }



Hope that's clear.

Replies

As for fadeOut of your previous post, but competion is different:


completionHandler: {
            self.view.window.myWindow.makeKeyAndOrderFront(nil)       // Make sure to make it disappear
            self.view.window.alphaValue = 1  // For next show of window
}


I do it without animator:

var windows (myWindow: NSWindow)          // the window to fadeIn

    func fadeInMyWindow() {
 
        DispatchQueue.main.async { //   UI
            self.myWindow.alphaValue = 0.0
            self.myWindow.makeKeyAndOrderFront(self.myWindowe)
        }
        let centiemeSeconde = UInt32(10_000)
        for i in 0...99 {
            usleep(centiemeSeconde)
            DispatchQueue.main.async { //  Car c'est une fonction UI ;
                self.myWindow.alphaValue = CGFloat(i) / 100
            }
        }
    }

I do not understand your code. I am totally lost. Can you explain where do you put each part? What steps do you take?

var myWindow: NSWindow // the window to fadeIn Declared for me as a global


func fadeInMyWindow() {


Is declared as a global func.


When I open ask to display the window :

        DispatchQueue.global(qos: .userInitiated).async {          //            in a thread for animation
            self.fadeInMyWindow()  // Show it
         }


Is it clear now ?

I suppose I do not have the necessary level. There are many things I do not understand in your code.


Just to begin: if I put the var it gives me an error: "Class 'ViewController' has no initializers"

Where should that var be? do you do anything before?


class ViewController: NSViewController {

var myWindow: NSWindow


}

OK, I'll show you a more complete set up.


1. I have a NSWindowController subclass

class SomeWindowController: NSWindowController, NSWindowDelegate, NSTextFieldDelegate {


2. Defined a window in IB and connected to an IBOutlet in SomeWindowController

    @IBOutlet fileprivate weak var myWindow          : NSWindow! // Non visible at launch


3. In this class, defined a fadeIn func

    func fadeInMyWindow() { 
  
        DispatchQueue.main.async { //   UI 
            self.myWindow.alphaValue = 0.0 
            self.myWindow.makeKeyAndOrderFront(self.myWindowe) 
        } 
        let centiemeSeconde = UInt32(10_000) 
        for i in 0...99 { 
            usleep(centiemeSeconde) 
            DispatchQueue.main.async { //  Car c'est une fonction UI ; 
                self.myWindow.alphaValue = CGFloat(i) / 100 
            } 
        } 
    }


4. On some button tap, I show the window


    @IBAction func launch(_ sender: NSButton) {
    
        myWindow.title = NSLocalizedString("MyWindow", comment: "Window")   // 18.3.2019
    
        DispatchQueue.global(qos: .userInitiated).async {
            self.fadeInMyWindow()  // Show window
        }

   }


And that's all.

Sorry, but I still do not understand.


Let's say that I create a new project:

The first window is ViewController.swift

I put a second View Controller from library. I connect that with SecondViewController.swift

I have button1 in ViewController.swift with segue1 that goes to SecondViewController.swift


I suppose the next step is, if I understand what you say, go to Main.storyboard control drag from SecondViewController to ViewController.swift and create an Oulet. Is that right? But if I amb in storyboard SecondViewController and I open the assistant, it will open the SecondViewController.swift not ViewController.swift. How and where do you make that connection of the window?

OK, here is the complete setup. I tested. May probably be optimized, but that's to show.


0. In the storyboard, I have 2 windowControllers, each with a viewController.

The second window controllr has an Identifier : S1W2


In the first window, I have a button that will call the second window and show with fadeIn.


1. In the VewController file for the FIRST window, define a var for the secondWindowController:


import Cocoa

class ViewController: NSViewController {

     var secondWindowController: NSWindowController? // could also be defined outside the class


2. Create a button in this VC of First window in the storyboard

No IBOutlet needed


3. Create the fadeIn function in class ViewController:


    func fadeInMyWindow() {
        print("Starts fade")     // Just to check it's called
        if self.secondWindowController == nil || self.secondWindowController!.window == nil { return }     // Better be cautious

        DispatchQueue.main.async { //   UI
            self.secondWindowController!.window!.alphaValue = 0.0
            self.secondWindowController!.window!.makeKeyAndOrderFront(nil)
        }
        let centiemeSeconde = UInt32(10_000)     // Can adjust the fade in speed
        for i in 0...99 {
            usleep(centiemeSeconde)     // I wait 1/100 second to have a visible effect
            DispatchQueue.main.async { // UI ;
                self.secondWindowController!.window!.alphaValue = CGFloat(i) / 100
            }
        }
    }


4. Connect the button to its IBAction from stroyboard to class ViewController ; connection is done by this action, bnot through a segue:


    @IBAction func ShowWindowByCode(_ sender: NSButton) {
     
        let storyboard = NSStoryboard(name: "Main", bundle: nil)
        if let windowController = storyboard.instantiateController(withIdentifier: "S1W2") as? NSWindowController
        {
            secondWindowController = windowController
            DispatchQueue.global(qos: .userInitiated).async {     // Needed for animation to occur
                self.fadeInMyWindow()
            }
        }
    }



Hope that's clear.

You're not doing yourself any favors trying to learn basics via a never ending forum thread.


Crawl, walk, run. Stop and learn the tool basics first, then jump into baby-stepping a simple app, then come here with questions based on what you learned. Don't run, stumble, fall down, then derp what happened...you'll be falling down for a long time while your goal moves away.


But if you insist, at lease use the Getting Started forum so anyone attempting to help doesn't waste time going over your head.