Skip to content

Instantly share code, notes, and snippets.

@MickaelCruzDB
Created June 7, 2015 09:54
Show Gist options
  • Save MickaelCruzDB/7a2463ffed625725c52b to your computer and use it in GitHub Desktop.
Save MickaelCruzDB/7a2463ffed625725c52b 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
bottomVC.view.wantsLayer = true
topVC.view.layerContentsRedrawPolicy = .OnSetNeedsDisplay
bottomVC.view.layerContentsRedrawPolicy = .OnSetNeedsDisplay
topVC.view.alphaValue = 0
bottomVC.view.addSubview(topVC.view)
NSAnimationContext.runAnimationGroup({ (context) -> Void in
context.duration = 1
var frame : NSRect = topVC.view.frame
frame.size = NSMakeSize(frame.size.width, frame.size.height)
// frame.origin.x = frame.size.width
// frame.origin.y = frame.size.height
// topVC.view.frame = frame
var frame2 : NSRect = bottomVC.view.frame
frame2.size = NSMakeSize(frame2.size.width, frame2.size.height)
//// frame2.origin.x = frame2.size.width
//// frame2.origin.y = frame2.size.height
// bottomVC.view.frame = frame2
// var frame : CGRect = NSRectToCGRect(topVC.view.frame)
topVC.view.animator().frame = CGRectOffset(frame, frame.size.width, 0) //Black
bottomVC.view.animator().frame = CGRectOffset(frame2, -(frame2.size.width), 0)
topVC.view.animator().alphaValue = 1
bottomVC.view.animator().alphaValue = 1
}, 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({ (context) -> Void in
context.duration = 1.5
var frame : CGRect = NSRectToCGRect(bottomVC.view.frame)
bottomVC.view.animator().frame = CGRectOffset(frame, -100, 0)
topVC.view.animator().frame = CGRectOffset(frame, 100, 0)
topVC.view.animator().alphaValue = 0
bottomVC.view.animator().alphaValue = 1
}, completionHandler: {
topVC.view.removeFromSuperview()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment