From 2606ac47f8cb3a2177b113fea1e4315b23e0a27d Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Thu, 12 Jul 2018 11:03:38 -0600 Subject: [PATCH] swipe gesture / code reorg --- .../MessageActionsViewController.swift | 76 +++++++++++-------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/Signal/src/ViewControllers/MessageActionsViewController.swift b/Signal/src/ViewControllers/MessageActionsViewController.swift index cd089eb71..57b1abab6 100644 --- a/Signal/src/ViewControllers/MessageActionsViewController.swift +++ b/Signal/src/ViewControllers/MessageActionsViewController.swift @@ -170,13 +170,42 @@ class MessageActionsViewController: UIViewController, MessageActionSheetDelegate let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapBackground)) self.view.addGestureRecognizer(tapGesture) - } - var snapshotView: UIView? + let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(didSwipeBackground)) + swipeGesture.direction = .down + self.view.addGestureRecognizer(swipeGesture) + } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(true) + self.animatePresentation() + } + + // MARK: Present / Dismiss animations + + var presentationFocusOffset: CGFloat? + var snapshotView: UIView? + + private func addSnapshotFocusedView() -> UIView? { + guard let snapshotView = self.focusedView.snapshotView(afterScreenUpdates: false) else { + owsFail("\(self.logTag) in \(#function) snapshotView was unexpectedly nil") + return nil + } + view.addSubview(snapshotView) + + guard let focusedViewSuperview = focusedView.superview else { + owsFail("\(self.logTag) in \(#function) focusedViewSuperview was unexpectedly nil") + return nil + } + + let convertedFrame = view.convert(focusedView.frame, from: focusedViewSuperview) + snapshotView.frame = convertedFrame + + return snapshotView + } + + private func animatePresentation() { // TODO first time only? guard let actionSheetViewVerticalConstraint = self.actionSheetViewVerticalConstraint else { @@ -203,8 +232,7 @@ class MessageActionsViewController: UIViewController, MessageActionSheetDelegate self.view.backgroundColor = UIColor.black.withAlphaComponent(0.4) } - // layout actionsheet and snapshot view with initial frame - self.view.layoutIfNeeded() + self.actionSheetView.superview?.layoutIfNeeded() let oldFocusFrame = self.view.convert(focusedView.frame, from: focusedViewSuperview) NSLayoutConstraint.deactivate([actionSheetViewVerticalConstraint]) @@ -232,32 +260,7 @@ class MessageActionsViewController: UIViewController, MessageActionSheetDelegate completion: nil) } - var presentationFocusOffset: CGFloat? - - private func addSnapshotFocusedView() -> UIView? { - guard let snapshotView = self.focusedView.snapshotView(afterScreenUpdates: false) else { - owsFail("\(self.logTag) in \(#function) snapshotView was unexpectedly nil") - return nil - } - view.addSubview(snapshotView) - - guard let focusedViewSuperview = focusedView.superview else { - owsFail("\(self.logTag) in \(#function) focusedViewSuperview was unexpectedly nil") - return nil - } - - let convertedFrame = view.convert(focusedView.frame, from: focusedViewSuperview) - snapshotView.frame = convertedFrame - - return snapshotView - } - - @objc - func didTapBackground() { - animateDismiss(action: nil) - } - - func animateDismiss(action: MessageAction?) { + private func animateDismiss(action: MessageAction?) { guard let actionSheetViewVerticalConstraint = self.actionSheetViewVerticalConstraint else { owsFail("\(self.logTag) in \(#function) actionSheetVerticalConstraint was unexpectedly nil") self.delegate?.messageActionsDidHide(self) @@ -301,6 +304,18 @@ class MessageActionsViewController: UIViewController, MessageActionSheetDelegate }) } + // MARK: Actions + + @objc + func didTapBackground() { + animateDismiss(action: nil) + } + + @objc + func didSwipeBackground(gesture: UISwipeGestureRecognizer) { + animateDismiss(action: nil) + } + // MARK: MessageActionSheetDelegate func actionSheet(_ actionSheet: MessageActionSheetView, didSelectAction action: MessageAction) { @@ -445,6 +460,7 @@ class MessageActionSheetView: UIView, MessageActionViewDelegate { } // MARK: + private func updateMask() { let cornerRadius: CGFloat = 16 let path: UIBezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))