From 35412e40474219a814e78a248895875b70a1a129 Mon Sep 17 00:00:00 2001 From: amagrude Date: Wed, 15 Nov 2017 12:53:53 -0500 Subject: [PATCH] Hooked up delegate calls & changed DetailViewController lifecycle. (#22) * Hooked up delegate protocol Added calls to cardWillShowDetailView, cardDidShowDetailView, cardWillCloseDetailView, cardDidCloseDetailView * Removed duplicate call to cardDidTapInside * detailView lifecycle changes. DetailViewController now owns only reference to the detailView. Removed reference to it from Card class. All DetailViewController setup happens inside of Card.shouldPresent call. Removed setup from Card.cardTapped() call. Changed how the card.backgroundIV is hooked upto the scrollView in DetailViewController. --- Cards/Sources/Card.swift | 12 ++++-------- Cards/Sources/CardGroup.swift | 10 ++-------- Cards/Sources/DetailViewController.swift | 19 +++++++++++++------ 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/Cards/Sources/Card.swift b/Cards/Sources/Card.swift index f7810ba..1d7a15d 100644 --- a/Cards/Sources/Card.swift +++ b/Cards/Sources/Card.swift @@ -99,7 +99,9 @@ import UIKit if let content = contentViewController { self.superVC = superVC detailVC.addChildViewController(content) - self.detailView = content.view + detailVC.detailView = content.view + detailVC.card = self + detailVC.delegate = self.delegate } } /** @@ -119,7 +121,6 @@ import UIKit //Private Vars fileprivate var tap = UITapGestureRecognizer() fileprivate var detailVC = DetailViewController() - fileprivate var detailView: UIView? var superVC: UIViewController? var originalFrame = CGRect.zero var backgroundIV = UIImageView() @@ -145,9 +146,6 @@ import UIKit tap.delegate = self tap.cancelsTouchesInView = false - if let detail = detailView { - detailVC.detailView = detail - } detailVC.transitioningDelegate = self // Adding Subviews @@ -189,12 +187,10 @@ import UIKit //MARK: - Actions - @objc func cardTapped(){ + @objc func cardTapped() { self.delegate?.cardDidTapInside?(card: self) if let vc = superVC { - - detailVC.detailView = detailView vc.present(self.detailVC, animated: true, completion: nil) } else { diff --git a/Cards/Sources/CardGroup.swift b/Cards/Sources/CardGroup.swift index bf761d9..0ab3f6c 100644 --- a/Cards/Sources/CardGroup.swift +++ b/Cards/Sources/CardGroup.swift @@ -61,7 +61,7 @@ import UIKit initialize() } - override func initialize() { + override func initialize() { super.initialize() vibrancyV = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: UIBlurEffect(style: blurEffect))) @@ -123,13 +123,7 @@ import UIKit height: gimme.Y(20)) titleLbl.sizeToFit() } - - - override func cardTapped() { - super.cardTapped() - delegate?.cardDidTapInside?(card: self) - - } + } diff --git a/Cards/Sources/DetailViewController.swift b/Cards/Sources/DetailViewController.swift index e898998..4a077ec 100644 --- a/Cards/Sources/DetailViewController.swift +++ b/Cards/Sources/DetailViewController.swift @@ -14,11 +14,8 @@ internal class DetailViewController: UIViewController { var scrollView = UIScrollView() var originalFrame = CGRect.zero var snap = UIView() - var card: Card! { - didSet{ - scrollView.addSubview(card.backgroundIV) - } - } + var card: Card! + var delegate: CardDelegate? //MARK: - View Lifecycle @@ -46,7 +43,12 @@ internal class DetailViewController: UIViewController { scrollView.showsVerticalScrollIndicator = false scrollView.showsHorizontalScrollIndicator = false } - + + override func viewWillAppear(_ animated: Bool) { + scrollView.addSubview(card.backgroundIV) + self.delegate?.cardWillShowDetailView?(card: self.card) + } + override func viewDidAppear(_ animated: Bool) { self.view.insertSubview(snap, belowSubview: blurView) originalFrame = scrollView.frame @@ -62,12 +64,17 @@ internal class DetailViewController: UIViewController { scrollView.contentSize = CGSize(width: scrollView.bounds.width, height: detail.frame.maxY) } + self.delegate?.cardDidShowDetailView?(card: self.card) } override func viewWillDisappear(_ animated: Bool) { + self.delegate?.cardWillCloseDetailView?(card: self.card) snap.removeFromSuperview() } + override func viewDidDisappear(_ animated: Bool) { + self.delegate?.cardDidCloseDetailView?(card: self.card) + } //MARK: - Layout & Animations for the content ( rect = Scrollview + card + detail )