From bb6722ea4212728b26b93d0fe2d81003142a196c Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 11 Jul 2018 10:53:02 -0600 Subject: [PATCH] animate in/out --- .../MessageActionsViewController.swift | 60 ++++++++++++++++++- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/SignalMessaging/ViewControllers/MessageActionsViewController.swift b/SignalMessaging/ViewControllers/MessageActionsViewController.swift index 748e55625..017819271 100644 --- a/SignalMessaging/ViewControllers/MessageActionsViewController.swift +++ b/SignalMessaging/ViewControllers/MessageActionsViewController.swift @@ -67,21 +67,53 @@ class MessageActionsViewController: UIViewController { fatalError("init(coder:) has not been implemented") } + var actionSheetViewVerticalConstraint: NSLayoutConstraint? + override func loadView() { self.view = UIView() - view.backgroundColor = UIColor.black.withAlphaComponent(0.4) highlightFocusedView() view.addSubview(actionSheetView) - actionSheetView.autoPinEdgesToSuperviewEdges(with: .zero, excludingEdge: .top) + + actionSheetView.autoPinWidthToSuperview() actionSheetView.setContentHuggingVerticalHigh() actionSheetView.setCompressionResistanceHigh() + self.actionSheetViewVerticalConstraint = actionSheetView.autoPinEdge(.top, to: .bottom, of: self.view) let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapBackground)) self.view.addGestureRecognizer(tapGesture) } + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(true) + + // TODO first time only? + + guard let actionSheetViewVerticalConstraint = self.actionSheetViewVerticalConstraint else { + owsFail("\(self.logTag) in \(#function) actionSheetViewVerticalConstraint was unexpectedly nil") + return + } + + // darken background + let backgroundDuration: TimeInterval = 0.1 + UIView.animate(withDuration: backgroundDuration) { + self.view.backgroundColor = UIColor.black.withAlphaComponent(0.4) + } + + // present action sheet + self.actionSheetView.superview?.layoutIfNeeded() + NSLayoutConstraint.deactivate([actionSheetViewVerticalConstraint]) + self.actionSheetViewVerticalConstraint = self.actionSheetView.autoPinEdge(toSuperviewEdge: .bottom) + UIView.animate(withDuration: 0.3, + delay: backgroundDuration, + options: .curveEaseOut, + animations: { + self.actionSheetView.superview?.layoutIfNeeded() + }, + completion: nil) + } + private func highlightFocusedView() { guard let snapshotView = self.focusedView.snapshotView(afterScreenUpdates: false) else { owsFail("\(self.logTag) in \(#function) snapshotView was unexpectedly nil") @@ -100,7 +132,29 @@ class MessageActionsViewController: UIViewController { @objc func didTapBackground() { - self.delegate?.messageActionsDidHide(self) + animateDismiss() + } + + func animateDismiss() { + self.actionSheetView.superview?.layoutIfNeeded() + + if let actionSheetViewVerticalConstraint = self.actionSheetViewVerticalConstraint { + NSLayoutConstraint.deactivate([actionSheetViewVerticalConstraint]) + } else { + owsFail("\(self.logTag) in \(#function) actionSheetVerticalConstraint was unexpectedly nil") + } + + self.actionSheetViewVerticalConstraint = self.actionSheetView.autoPinEdge(.top, to: .bottom, of: self.view) + UIView.animate(withDuration: 0.2, + delay: 0, + options: .curveEaseOut, + animations: { + self.view.backgroundColor = UIColor.clear + self.actionSheetView.superview?.layoutIfNeeded() + }, + completion: { _ in + self.delegate?.messageActionsDidHide(self) + }) } }