swiftanimationnsanimationcontext

MacOS Animation delay in swift


Is it possible to add a delay to an animation with Cocoa? In my current code it shows a window and hides it with fade animation. What I am trying to do is add a delay before the fade animation.

   @IBAction func doIt(_ sender: NSButton) {
       openPanel()

       NSAnimationContext.runAnimationGroup { (cont) in
           cont.duration = 1.0
           self.panel.animator().alphaValue = 0
       }
       //hide on completion
   }

Solution

  • Wrap what you want to delay in a DispathQueue async call:

    DispatchQueue.main.asyncAfter(deadline: .now() + 1) { //delays 1 second
      //code to delay            
    }