Skip to content

Commit

Permalink
Demo project improved + Bug Fixes
Browse files Browse the repository at this point in the history
CardPlayer behaviour fix
Delegate system clean up
Attributes description added
Slight animations improvements
Code cleanup
  • Loading branch information
PaoloCuscela committed Oct 31, 2017
1 parent 7a3a9c2 commit c844271
Show file tree
Hide file tree
Showing 18 changed files with 920 additions and 313 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.4'
s.version = '1.2.5'
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
16 changes: 11 additions & 5 deletions Cards/Sources/Animator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Animator: NSObject, UIViewControllerAnimatedTransitioning {

fileprivate var presenting: Bool
fileprivate var velocity = 0.6
var bounceIntensity: CGFloat = 0.1
var bounceIntensity: CGFloat = 0.07
var card: Card

init(presenting: Bool, from card: Card) {
Expand All @@ -34,21 +34,24 @@ class Animator: NSObject, UIViewControllerAnimatedTransitioning {
guard presenting else {

// Detail View Controller Dismiss Animations
card.isPresenting = false

let superVC = to
let detailVC = from as! DetailViewController
let cardBackgroundFrame = detailVC.scrollView.convert(detailVC.cardBackground.frame, to: nil)
let cardBackgroundFrame = detailVC.scrollView.convert(card.backgroundIV.frame, to: nil)
let bounce = self.bounceTransform(cardBackgroundFrame, to: card.originalFrame)

// Blur and fade with completion
UIView.animate(withDuration: velocity, delay: 0, options: .curveEaseOut, animations: {

detailVC.blurView.alpha = 0
detailVC.snap.alpha = 0
self.card.backgroundIV.layer.cornerRadius = self.card.cardRadius

}, completion: { _ in

self.card.addSubview(detailVC.cardBackground)
detailVC.layout(self.card.originalFrame, isPresenting: false, isAnimating: false)
self.card.addSubview(detailVC.card.backgroundIV)
transitionContext.completeTransition(true)
})

Expand All @@ -69,12 +72,13 @@ class Animator: NSObject, UIViewControllerAnimatedTransitioning {
}

// Detail View Controller Present Animations
card.isPresenting = true

let detailVC = to as! DetailViewController
let bounce = self.bounceTransform(card.originalFrame, to: UIScreen.main.bounds)
let bounce = self.bounceTransform(card.originalFrame, to: card.backgroundIV.frame)

container.bringSubview(toFront: detailVC.view)
detailVC.cardBackground = card.backgroundIV
detailVC.card = card
detailVC.layout(card.originalFrame, isPresenting: false)

// Blur and fade with completion
Expand All @@ -83,9 +87,11 @@ class Animator: NSObject, UIViewControllerAnimatedTransitioning {
self.card.transform = CGAffineTransform.identity // Reset card identity after push back on tap
detailVC.blurView.alpha = 1
detailVC.snap.alpha = 1
self.card.backgroundIV.layer.cornerRadius = 0

}, completion: { _ in

detailVC.layout(self.card.originalFrame, isPresenting: true, isAnimating: false, transform: .identity)
transitionContext.completeTransition(true)
})

Expand Down
96 changes: 70 additions & 26 deletions Cards/Sources/Card.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,87 @@ import UIKit
@IBDesignable open class Card: UIView, CardDelegate {

// Storyboard Inspectable vars
@IBInspectable public var shadowBlur: CGFloat = 14
@IBInspectable public var shadowOpacity: Float = 0.6
@IBInspectable public var shadowColor: UIColor = UIColor.gray
@IBInspectable public var backgroundImage: UIImage?
/**
Color for the card's labels.
*/
@IBInspectable public var textColor: UIColor = UIColor.black
@IBInspectable public var cardRadius: CGFloat = 20
/**
Amount of blur for the card's shadow.
*/
@IBInspectable public var shadowBlur: CGFloat = 14 {
didSet{
self.layer.shadowRadius = shadowBlur
}
}
/**
Alpha of the card's shadow.
*/
@IBInspectable public var shadowOpacity: Float = 0.6 {
didSet{
self.layer.shadowOpacity = shadowOpacity
}
}
/**
Color of the card's shadow.
*/
@IBInspectable public var shadowColor: UIColor = UIColor.gray {
didSet{
self.layer.shadowColor = shadowColor.cgColor
}
}
/**
The image to display in the background.
*/
@IBInspectable public var backgroundImage: UIImage? {
didSet{
self.backgroundIV.image = backgroundImage
}
}
/**
Corner radius of the card.
*/
@IBInspectable public var cardRadius: CGFloat = 20{
didSet{
self.layer.cornerRadius = cardRadius
}
}
/**
Insets between card's content and edges ( in percentage )
*/
@IBInspectable public var contentInset: CGFloat = 6 {
didSet {
insets = LayoutHelper(rect: originalFrame).X(contentInset)
}
}

/**
Color of the card's background.
*/
override open var backgroundColor: UIColor? {
didSet(new) {
if let color = new { backgroundIV.backgroundColor = color }
if backgroundColor != UIColor.clear { backgroundColor = UIColor.clear }
}
}

/**
detailView -> The view to show after presenting detail; from -> Your current ViewController (self)
*/
public func shouldPresent( _ detailView: UIView? = nil, from superVC: UIViewController? = nil) {
self.superVC = superVC
self.detailView = detailView
}

/**
If the card should display parallax effect.
*/
public var hasParallax: Bool = true {
didSet {
if self.motionEffects.isEmpty && hasParallax { goParallax() }
else if !hasParallax && !motionEffects.isEmpty { motionEffects.removeAll() }
}
}

var delegate: CardDelegate?
/**
Delegate for the card. Should extend your VC with CardDelegate.
*/
public var delegate: CardDelegate?

//Private Vars
fileprivate var tap = UITapGestureRecognizer()
Expand All @@ -74,6 +120,7 @@ import UIKit
var originalFrame = CGRect.zero
var backgroundIV = UIImageView()
var insets = CGFloat()
var isPresenting = false

//MARK: - View Life Cycle

Expand Down Expand Up @@ -102,13 +149,17 @@ import UIKit
// Adding Subviews
self.addSubview(backgroundIV)

delegate = self
backgroundIV.isUserInteractionEnabled = true
self.backgroundColor = UIColor.white

if backgroundIV.backgroundColor == nil {
backgroundIV.backgroundColor = UIColor.white
super.backgroundColor = UIColor.clear
}
}

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

self.layer.shadowOpacity = shadowOpacity
self.layer.shadowColor = shadowColor.cgColor
Expand All @@ -121,33 +172,29 @@ import UIKit
backgroundIV.clipsToBounds = true
backgroundIV.contentMode = .scaleAspectFill

layout(rect)

originalFrame = rect
backgroundIV.frame.origin = bounds.origin
backgroundIV.frame.size = CGSize(width: bounds.width, height: bounds.height)
contentInset = 6
}


//MARK: - Layout

func layout(_ rect: CGRect){

backgroundIV.frame.origin = rect.origin
backgroundIV.frame.size = CGSize(width: rect.width, height: rect.height)
}
func layout(animating: Bool = true){ }


//MARK: - Actions

@objc func cardTapped(){
self.delegate?.cardDidTapInside?(card: self)

if let vc = superVC {

detailVC.detailView = detailView
vc.present(self.detailVC, animated: true, completion: nil)
} else {

resetAnimated { }
resetAnimated()
}
}

Expand All @@ -159,12 +206,9 @@ import UIKit
UIView.animate(withDuration: 0.2, animations: { self.transform = CGAffineTransform(scaleX: 0.95, y: 0.95) })
}

private func resetAnimated(_ completion: @escaping () -> ()) {
private func resetAnimated() {

UIView.animate(withDuration: 0.2, animations: { self.transform = CGAffineTransform.identity }) { _ in
self.delegate?.cardDidTapInside?(card: self)
completion()
}
UIView.animate(withDuration: 0.2, animations: { self.transform = CGAffineTransform.identity })
}

func goParallax() {
Expand Down
42 changes: 20 additions & 22 deletions Cards/Sources/CardArticle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,26 @@ import UIKit
@IBDesignable open class CardArticle: Card {

// SB Vars
/**
Text of the title label.
*/
@IBInspectable public var title: String = "The Art of the Impossible"
/**
Max font size the title label.
*/
@IBInspectable public var titleSize: CGFloat = 26
/**
Text of the subtitle label.
*/
@IBInspectable public var subtitle: String = "Inside the extraordinary world of Monument Valley 2"
/**
Max font size the subtitle label.
*/
@IBInspectable public var subtitleSize: CGFloat = 17
/**
Text of the category label.
*/
@IBInspectable public var category: String = "world premiere"
@IBInspectable public var blurEffect: UIBlurEffectStyle = UIBlurEffectStyle.extraLight

//Priv Vars
var titleLbl = UILabel ()
Expand All @@ -36,7 +50,6 @@ import UIKit
override func initialize() {
super.initialize()

self.delegate = self
backgroundIV.addSubview(titleLbl)
backgroundIV.addSubview(subtitleLbl)
backgroundIV.addSubview(categoryLbl)
Expand Down Expand Up @@ -78,21 +91,14 @@ import UIKit
subtitleLbl.numberOfLines = 0
subtitleLbl.textAlignment = .left

self.layout(backgroundIV.frame)
self.layout()

}

override func cardTapped() {
super.cardTapped()
delegate?.cardDidTapInside?(card: self)
}


override func layout(_ rect: CGRect) {
override func layout(animating: Bool = true) {
super.layout(animating: animating)

super.layout(rect)

let gimme = LayoutHelper(rect: rect)
let gimme = LayoutHelper(rect: backgroundIV.bounds)

categoryLbl.frame = CGRect(x: insets,
y: insets,
Expand All @@ -103,23 +109,15 @@ import UIKit
y: gimme.Y(1, from: categoryLbl),
width: gimme.X(80),
height: gimme.Y(17))

subtitleLbl.frame = CGRect(x: insets,
y: gimme.RevY(0, height: gimme.Y(14)) - insets,
width: gimme.X(80),
height: gimme.Y(14))
titleLbl.sizeToFit()

}

}

extension CardArticle {

public func cardDidShowDetailView(card: Card) { layout(backgroundIV.bounds) }
public func cardWillCloseDetailView(card: Card) { layout(originalFrame) }

}



Loading

0 comments on commit c844271

Please sign in to comment.