Skip to content

Commit

Permalink
Add fullscreen for detail view
Browse files Browse the repository at this point in the history
  • Loading branch information
PaoloCuscela committed Nov 19, 2017
1 parent 35412e4 commit db67568
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Cards.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Cards'
s.version = '1.2.6'
s.version = '1.3.0'
s.summary = 'Awesome iOS 11 appstore cards in swift 4.'
s.homepage = 'https://github.com/PaoloCuscela/Cards'
s.screenshots = 'https://raw.githubusercontent.com/PaoloCuscela/Cards/master/Images/Overview.png', 'https://raw.githubusercontent.com/PaoloCuscela/Cards/master/Images/CardGroupSliding.gif'
Expand Down
1 change: 0 additions & 1 deletion Cards/Sources/Animator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class Animator: NSObject, UIViewControllerAnimatedTransitioning {
// Detail View Controller Dismiss Animations
card.isPresenting = false

let superVC = to
let detailVC = from as! DetailViewController
let cardBackgroundFrame = detailVC.scrollView.convert(card.backgroundIV.frame, to: nil)
let bounce = self.bounceTransform(cardBackgroundFrame, to: card.originalFrame)
Expand Down
3 changes: 2 additions & 1 deletion Cards/Sources/Card.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ import UIKit
contentViewController -> The view controller to present when the card is tapped
from -> Your current ViewController (self)
*/
public func shouldPresent( _ contentViewController: UIViewController? = nil, from superVC: UIViewController? = nil) {
public func shouldPresent( _ contentViewController: UIViewController?, from superVC: UIViewController?, fullscreen: Bool = false) {
if let content = contentViewController {
self.superVC = superVC
detailVC.addChildViewController(content)
detailVC.detailView = content.view
detailVC.card = self
detailVC.delegate = self.delegate
detailVC.isFullscreen = fullscreen
}
}
/**
Expand Down
2 changes: 1 addition & 1 deletion Cards/Sources/CardHighlight.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ import UIKit
let btnTitle = NSAttributedString(string: buttonText.uppercased(), attributes: [ NSAttributedStringKey.font : UIFont.systemFont(ofSize: 16, weight: .black), NSAttributedStringKey.foregroundColor : self.tintColor])
actionBtn.setAttributedTitle(btnTitle, for: .normal)

btnWidth = CGFloat((buttonText.characters.count + 2) * 10)
btnWidth = CGFloat((buttonText.count + 2) * 10)

layout()

Expand Down
114 changes: 108 additions & 6 deletions Cards/Sources/DetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ internal class DetailViewController: UIViewController {
var snap = UIView()
var card: Card!
var delegate: CardDelegate?
var isFullscreen = false

fileprivate var xButton = XButton()

//MARK: - View Lifecycle

Expand All @@ -26,33 +28,47 @@ internal class DetailViewController: UIViewController {
self.snap = UIScreen.main.snapshotView(afterScreenUpdates: true)
self.view.addSubview(blurView)
self.view.addSubview(scrollView)

if let detail = detailView {

scrollView.addSubview(detail)
detail.autoresizingMask = .flexibleWidth
detail.alpha = 0
detail.autoresizingMask = .flexibleWidth
}

blurView.frame = self.view.bounds

scrollView.layer.backgroundColor = detailView?.backgroundColor?.cgColor ?? UIColor.white.cgColor
scrollView.layer.cornerRadius = 20
scrollView.layer.cornerRadius = isFullscreen ? 0 : 20

scrollView.delegate = self
scrollView.alwaysBounceVertical = true
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false

xButton.addTarget(self, action: #selector(dismissVC), for: .touchUpInside)
blurView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissVC)))
xButton.isUserInteractionEnabled = true
view.isUserInteractionEnabled = true

}


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

if isFullscreen {
view.addSubview(xButton)
}

view.insertSubview(snap, belowSubview: blurView)

if let detail = detailView {

detail.alpha = 1
Expand All @@ -62,20 +78,32 @@ internal class DetailViewController: UIViewController {
height: detail.frame.height)

scrollView.contentSize = CGSize(width: scrollView.bounds.width, height: detail.frame.maxY)


xButton.frame = CGRect (x: scrollView.frame.maxX - 20 - 40,
y: scrollView.frame.minY + 20,
width: 40,
height: 40)



}

self.delegate?.cardDidShowDetailView?(card: self.card)
}

override func viewWillDisappear(_ animated: Bool) {
self.delegate?.cardWillCloseDetailView?(card: self.card)
detailView?.alpha = 0
snap.removeFromSuperview()
xButton.removeFromSuperview()
}

override func viewDidDisappear(_ animated: Bool) {
self.delegate?.cardDidCloseDetailView?(card: self.card)
}


//MARK: - Layout & Animations for the content ( rect = Scrollview + card + detail )

func layout(_ rect: CGRect, isPresenting: Bool, isAnimating: Bool = true, transform: CGAffineTransform = CGAffineTransform.identity){
Expand All @@ -88,9 +116,15 @@ internal class DetailViewController: UIViewController {
return
}

scrollView.frame.size = CGSize(width: LayoutHelper.XScreen(85), height: LayoutHelper.YScreen(100) - 20)
scrollView.center = blurView.center
scrollView.frame.origin.y = 40
if isFullscreen {
scrollView.frame = view.frame

} else {
scrollView.frame.size = CGSize(width: LayoutHelper.XScreen(85), height: LayoutHelper.YScreen(100) - 20)
scrollView.center = blurView.center
scrollView.frame.origin.y = 40
}

scrollView.frame = scrollView.frame.applying(transform)

card.backgroundIV.frame.origin = scrollView.bounds.origin
Expand All @@ -99,6 +133,14 @@ internal class DetailViewController: UIViewController {
card.layout(animating: isAnimating)

}


//MARK: - Actions

@objc func dismissVC(){
scrollView.contentOffset.y = 0
dismiss(animated: true, completion: nil)
}
}


Expand All @@ -112,8 +154,11 @@ extension DetailViewController: UIScrollViewDelegate {
let origin = originalFrame.origin.y
let currentOrigin = originalFrame.origin.y

xButton.alpha = y - (card.backgroundIV.bounds.height * 0.6)

if (y<0 || currentOrigin > origin) {
scrollView.frame.origin.y -= y/2

scrollView.contentOffset.y = 0
}

Expand Down Expand Up @@ -145,4 +190,61 @@ extension DetailViewController: UIScrollViewDelegate {

}

class XButton: UIButton {

private let circle = UIVisualEffectView(effect: UIBlurEffect(style: .dark))

override var frame: CGRect {
didSet{
setNeedsDisplay()
}
}

override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(circle)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override func draw(_ rect: CGRect) {
super.draw(rect)

let xPath = UIBezierPath()
let xLayer = CAShapeLayer()
let inset = rect.width * 0.3

xPath.move(to: CGPoint(x: inset, y: inset))
xPath.addLine(to: CGPoint(x: rect.maxX - inset, y: rect.maxY - inset))

xPath.move(to: CGPoint(x: rect.maxX - inset, y: inset))
xPath.addLine(to: CGPoint(x: inset, y: rect.maxY - inset))

xLayer.path = xPath.cgPath

xLayer.strokeColor = UIColor.white.cgColor
xLayer.lineWidth = 2.0
self.layer.addSublayer(xLayer)

circle.frame = rect
circle.layer.cornerRadius = circle.bounds.width / 2
circle.clipsToBounds = true
circle.isUserInteractionEnabled = false


}


}










Loading

0 comments on commit db67568

Please sign in to comment.