Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. //
  2. // AddShowTransitionAnimator.swift
  3. // TVShowForecast
  4. //
  5. // Created by Thomas Krajacic on 3.11.2014.
  6. // Copyright (c) 2014 Thomas Krajacic. All rights reserved.
  7. //
  8.  
  9. import Cocoa
  10.  
  11. class FadeTransitionAnimator: NSObject, NSViewControllerPresentationAnimator {
  12.  
  13. var hConstraints : [AnyObject]?
  14. var vConstraints : [AnyObject]?
  15.  
  16. func animatePresentationOfViewController(viewController: NSViewController, fromViewController: NSViewController) {
  17.  
  18. let bottomVC = fromViewController as NSViewController
  19. let topVC = viewController as NSViewController
  20.  
  21. // make sure the view has a CA layer for smooth animation
  22. topVC.view.wantsLayer = true
  23.  
  24. // set redraw policy
  25. topVC.view.layerContentsRedrawPolicy = .OnSetNeedsDisplay
  26.  
  27. // switch to autolayout for resizing
  28. topVC.view.translatesAutoresizingMaskIntoConstraints = false
  29.  
  30. // hide view initially
  31. topVC.view.alphaValue = 0.0
  32.  
  33. // add view of presented viewcontroller
  34. bottomVC.view.addSubview(topVC.view)
  35.  
  36. // add constraints
  37. self.hConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[top]|", options: nil, metrics: nil, views: ["top":topVC.view])
  38. self.vConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|[top]|", options: nil, metrics: nil, views: ["top":topVC.view])
  39.  
  40. bottomVC.view.addConstraints(self.hConstraints!)
  41. bottomVC.view.addConstraints(self.vConstraints!)
  42.  
  43. // Do some CoreAnimation stuff to present view
  44. NSAnimationContext.runAnimationGroup({ (context) -> Void in
  45.  
  46. context.allowsImplicitAnimation = true
  47.  
  48. // fade duration
  49. context.duration = 1.3
  50. // move into place
  51. topVC.view.alphaValue = 1.0
  52.  
  53.  
  54. var transition:CATransition = CATransition()
  55. transition.type = kCATransitionMoveIn
  56. transition.subtype = kCATransitionFromRight
  57. transition.delegate = self
  58.  
  59.  
  60. }, completionHandler: {
  61.  
  62.  
  63. })
  64.  
  65. }
  66.  
  67. func animateDismissalOfViewController(viewController: NSViewController, fromViewController: NSViewController) {
  68.  
  69. let bottomVC = fromViewController as NSViewController
  70. let topVC = viewController as NSViewController
  71.  
  72. // make sure the view has a CA layer for smooth animation
  73. topVC.view.wantsLayer = true
  74.  
  75. // set redraw policy
  76. topVC.view.layerContentsRedrawPolicy = .OnSetNeedsDisplay
  77.  
  78. // Do some CoreAnimation stuff to present view
  79. NSAnimationContext.runAnimationGroup({ (context) -> Void in
  80.  
  81. context.allowsImplicitAnimation = true
  82.  
  83. // fade duration
  84. context.duration = 1.3
  85.  
  86. //move top view to the left
  87. topVC.view.alphaValue = 0.0
  88.  
  89. }, completionHandler: {
  90.  
  91. // remove view
  92. topVC.view.removeFromSuperview()
  93. })
  94.  
  95. // The VC is deallocated
  96. }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement