Skip to content

Instantly share code, notes, and snippets.

@OskarGroth
Last active August 19, 2019 12:51
Show Gist options
  • Save OskarGroth/c1d5a99b44543b0f4aef9776dddcf3c5 to your computer and use it in GitHub Desktop.
Save OskarGroth/c1d5a99b44543b0f4aef9776dddcf3c5 to your computer and use it in GitHub Desktop.
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()
})
}
}
@OskarGroth
Copy link
Author

Output

Skärmavbild 2019-08-19 kl  14 50 54

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment