Skip to content

Instantly share code, notes, and snippets.

@MickaelCruzDB
Created June 7, 2015 15:13
Show Gist options
  • Save MickaelCruzDB/72de44fb60c4fb3f57d6 to your computer and use it in GitHub Desktop.
Save MickaelCruzDB/72de44fb60c4fb3f57d6 to your computer and use it in GitHub Desktop.
//
// AddShowTransitionAnimator.swift
// TVShowForecast
//
// Created by Thomas Krajacic on 3.11.2014.
// Copyright (c) 2014 Thomas Krajacic. All rights reserved.
//
import Cocoa
class FadeTransitionAnimator: NSObject, NSViewControllerPresentationAnimator {
var hConstraints : [AnyObject]?
var vConstraints : [AnyObject]?
func animatePresentationOfViewController(viewController: NSViewController, fromViewController: NSViewController) {
let bottomVC = fromViewController as NSViewController
let topVC = viewController as NSViewController
// make sure the view has a CA layer for smooth animation
topVC.view.wantsLayer = true
// set redraw policy
topVC.view.layerContentsRedrawPolicy = .OnSetNeedsDisplay
// switch to autolayout for resizing
topVC.view.translatesAutoresizingMaskIntoConstraints = false
// hide view initially
topVC.view.alphaValue = 0.0
// add view of presented viewcontroller
bottomVC.view.addSubview(topVC.view)
// add constraints
self.hConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[top]|", options: nil, metrics: nil, views: ["top":topVC.view])
self.vConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|[top]|", options: nil, metrics: nil, views: ["top":topVC.view])
bottomVC.view.addConstraints(self.hConstraints!)
bottomVC.view.addConstraints(self.vConstraints!)
// Do some CoreAnimation stuff to present view
NSAnimationContext.runAnimationGroup({ (context) -> Void in
context.allowsImplicitAnimation = true
// fade duration
context.duration = 1.3
// move into place
topVC.view.alphaValue = 1.0
var transition:CATransition = CATransition()
transition.type = kCATransitionMoveIn
transition.subtype = kCATransitionFromRight
transition.delegate = self
topVC.transpose(transition)
}, completionHandler: {
})
}
func animateDismissalOfViewController(viewController: NSViewController, fromViewController: NSViewController) {
let bottomVC = fromViewController as NSViewController
let topVC = viewController as NSViewController
// make sure the view has a CA layer for smooth animation
topVC.view.wantsLayer = true
// set redraw policy
topVC.view.layerContentsRedrawPolicy = .OnSetNeedsDisplay
// Do some CoreAnimation stuff to present view
NSAnimationContext.runAnimationGroup({ (context) -> Void in
context.allowsImplicitAnimation = true
// fade duration
context.duration = 1.3
//move top view to the left
topVC.view.alphaValue = 0.0
}, completionHandler: {
// remove view
topVC.view.removeFromSuperview()
})
// The VC is deallocated
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment