Skip to content

Instantly share code, notes, and snippets.

@MickaelCruzDB
Created June 5, 2015 10:13
Show Gist options
  • Save MickaelCruzDB/d8455e12dd6ea7ccb691 to your computer and use it in GitHub Desktop.
Save MickaelCruzDB/d8455e12dd6ea7ccb691 to your computer and use it in GitHub Desktop.
import Cocoa
import AppKit
class MyCustomSwiftAnimator: NSObject, NSViewControllerPresentationAnimator {
@objc func animatePresentationOfViewController(viewController: NSViewController, fromViewController: NSViewController) {
let bottomVC = fromViewController
let topVC = viewController
topVC.view.wantsLayer = true
topVC.view.alphaValue = 0
bottomVC.view.addSubview(topVC.view)
var transition:CATransition = CATransition()
transition.duration = 0.3
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
transition.delegate = self
NSAnimationContext.runAnimationGroup({ (transition) -> Void in
transition.duration = 0.5
let existingView = NSView.self
let nextView = NSView.self
topVC.view.animator().replaceSubview(existingView.self(), with: nextView.self())
topVC.view.animator().alphaValue = 1
bottomVC.view.animator().alphaValue = 0
}, completionHandler: nil)
}
@objc func animateDismissalOfViewController(viewController: NSViewController, fromViewController: NSViewController) {
let bottomVC = fromViewController
let topVC = viewController
topVC.view.wantsLayer = true
topVC.view.layerContentsRedrawPolicy = .OnSetNeedsDisplay
NSAnimationContext.runAnimationGroup({ (transition) -> Void in
transition.duration = 0.5
let existingView = NSView.self
let nextView = NSView.self
topVC.view.animator().replaceSubview(existingView.self(), with: nextView.self())
topVC.view.animator().alphaValue = 0
bottomVC.view.animator().alphaValue = 1
}, completionHandler: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment