I have a simple Swift macOS app (using Xcode 8.2.1) that contains a single NSButton. When I click the button I would like it to fade out over a specified period. I thought I could use NSAnimationContext
but no matter what value I set the context
duration the button fades out almost immediately. Is this not the right way to do this?
class ViewController: NSViewController {
@IBOutlet weak var basicButton: NSButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func basicButtonClicked(_ sender: NSButton) {
NSAnimationContext.runAnimationGroup({ (context) in
context.duration = 10.0
self.basicButton.animator().alphaValue = 1
}) {
self.basicButton.animator().alphaValue = 0
}
}
}
I misunderstood how the animator values worked during the animation. The right way to set this up is:
@IBAction func basicButtonClicked(_ sender: NSButton) {
NSAnimationContext.runAnimationGroup({ (context) in
context.duration = 10.0
// Use the value you want to animate to (NOT the starting value)
self.basicButton.animator().alphaValue = 0
})
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With