Last active
August 19, 2019 12:51
-
-
Save OskarGroth/c1d5a99b44543b0f4aef9776dddcf3c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SheetAnimator: NSObject, NSViewControllerPresentationAnimator { | |
private var animationUUID: UUID? | |
public func animatePresentation(of viewController: NSViewController, from fromViewController: NSViewController) { | |
let containerView = fromViewController.view | |
let sheetView = viewController.view | |
let uuid = UUID() | |
animationUUID = uuid | |
print("\nSheet view origin x was \(sheetView.frame.origin.x)") | |
containerView.addSubview(sheetView) | |
sheetView.frame.origin = CGPoint(x: (containerView.bounds.width - sheetView.bounds.width)/2, y: -sheetView.frame.height) | |
print("Sheet presentation origin x was \((sheetView.layer!.presentation()?.frame.origin.x))") | |
print("Container view width was \(containerView.bounds.width)") | |
print("Sheet origin x was set to \(sheetView.frame.origin.x) with width \(sheetView.bounds.width)") | |
NSAnimationContext.runAnimationGroup({ (context) -> Void in | |
context.duration = NSView.defaultAnimationDuration | |
sheetView.animator().frame.origin.y = 0 | |
}, completionHandler: { [weak self] in | |
guard uuid == self?.animationUUID else { return } | |
NSLayoutConstraint.activate([ | |
sheetView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor), | |
sheetView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor) | |
]) | |
}) | |
} | |
public func animateDismissal(of viewController: NSViewController, from fromViewController: NSViewController) { | |
let sheetView = viewController.view | |
let uuid = UUID() | |
animationUUID = uuid | |
sheetView.immediateConstraintsAffectingSelf.deactivate() | |
NSAnimationContext.runAnimationGroup({ (context) -> Void in | |
context.duration = NSView.defaultAnimationDuration | |
sheetView.animator().frame.origin.y = -sheetView.frame.height | |
}, completionHandler: { [weak self] in | |
guard uuid == self?.animationUUID else { return } | |
print("Removed") | |
sheetView.removeFromSuperview() | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output