Skip to content

Commit 4c90e43

Browse files
committed
Externalize configuration and data retrieval (not implemented yet)
1 parent 5b68844 commit 4c90e43

File tree

2 files changed

+96
-54
lines changed

2 files changed

+96
-54
lines changed

CardAnimation/AnimatedCardView/AnimatedCardsView.swift

+77-44
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,46 @@
99
import UIKit
1010

1111
public protocol AnimatedCardsViewDataSource : class {
12-
12+
func numberOfVisibleCards() -> Int
13+
func numberOfCards() -> Int
14+
func contentForCardNumber(number:Int, size:(width:CGFloat, height:CGFloat)) -> UIView
1315
}
1416

1517
public class AnimatedCardsView: UIView {
1618

1719
private var cardArray : [UIView]!
20+
private lazy var gestureRecognizer : UIPanGestureRecognizer = {
21+
return UIPanGestureRecognizer(target: self, action: "scrollOnView:")
22+
}()
1823

19-
public weak var dataSource : AnimatedCardsViewDataSource?
24+
public weak var dataSourceDelegate : AnimatedCardsViewDataSource? {
25+
didSet {
26+
if dataSourceDelegate != nil {
27+
configure()
28+
}
29+
}
30+
}
2031

2132
public struct Constants {
2233
struct DefaultSize {
2334
static let width : CGFloat = 400.0
2435
static let ratio : CGFloat = 3.0 / 4.0
2536
}
26-
static let numberOfCards = 8
37+
}
38+
39+
private struct PrivateConstants {
40+
static let maxVisibleCardCount = 8
41+
static let cardCount = 8
2742
}
2843

2944
var frontCardTag = 1
30-
var cardCount = 0
31-
let maxVisibleCardCount = 8
45+
var cardCount = PrivateConstants.cardCount
46+
var maxVisibleCardCount = PrivateConstants.maxVisibleCardCount
3247
let gradientBackgroundLayer = CAGradientLayer()
3348
var gestureDirection:panScrollDirection = .Up
3449

50+
51+
// MARK: Initializers
3552
override init(frame: CGRect) {
3653
cardArray = []
3754
super.init(frame: frame)
@@ -45,53 +62,23 @@ public class AnimatedCardsView: UIView {
4562
configure()
4663
}
4764

65+
// MARK: Config
4866
private func configure() {
4967
generateCards()
50-
let scrollGesture = UIPanGestureRecognizer(target: self, action: "scrollOnView:")
51-
self.addGestureRecognizer(scrollGesture)
52-
cardCount = Constants.numberOfCards
68+
configureConstants()
69+
addGestureRecognizer(gestureRecognizer)
5370
relayoutSubViews()
5471
}
5572

56-
57-
// MARK: Private stuff
58-
59-
private func generateCards() {
60-
cardArray = (0...Constants.numberOfCards).map { (tagId) in
61-
let view = generateNewCardViewWithTagId(tagId)
62-
self.addSubview(view)
63-
applyConstraintsToView(view)
64-
return view
65-
}
73+
private func configureConstants() {
74+
maxVisibleCardCount = self.dataSourceDelegate?.numberOfVisibleCards() ?? PrivateConstants.maxVisibleCardCount
75+
cardCount = self.dataSourceDelegate?.numberOfCards() ?? PrivateConstants.cardCount
6676
}
6777

68-
private func generateNewCardViewWithTagId(tagId:NSInteger) -> UIView {
69-
let view = UIView()
70-
view.translatesAutoresizingMaskIntoConstraints = false
71-
view.tag = tagId+1
72-
switch tagId {
73-
case 0: view.backgroundColor = UIColor.purpleColor()
74-
case 1: view.backgroundColor = UIColor.redColor()
75-
case 2: view.backgroundColor = UIColor.blackColor()
76-
case 3: view.backgroundColor = UIColor.greenColor()
77-
case 4: view.backgroundColor = UIColor.brownColor()
78-
case 5: view.backgroundColor = UIColor.darkGrayColor()
79-
case 6: view.backgroundColor = UIColor.blueColor()
80-
case 7: view.backgroundColor = UIColor.orangeColor()
81-
default: view.backgroundColor = UIColor.whiteColor()
82-
}
83-
return view
84-
}
78+
// MARK: Public
8579

86-
private func applyConstraintsToView(view:UIView) {
87-
view.addConstraints([
88-
NSLayoutConstraint(item: view, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: CGFloat(1.0), constant: Constants.DefaultSize.width),
89-
NSLayoutConstraint(item: view, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: Constants.DefaultSize.ratio, constant: 0),
90-
])
91-
view.superview!.addConstraints([
92-
NSLayoutConstraint(item: view, attribute: .CenterX, relatedBy: .Equal, toItem: view.superview, attribute: .CenterX, multiplier: CGFloat(1.0), constant: 0),
93-
NSLayoutConstraint(item: view, attribute: .CenterY, relatedBy: .Equal, toItem: view.superview, attribute: .CenterY, multiplier: CGFloat(1.0), constant: 0),
94-
])
80+
public func reloadData() {
81+
configure()
9582
}
9683

9784
public func flipUp() {
@@ -161,6 +148,52 @@ public class AnimatedCardsView: UIView {
161148

162149
}
163150

151+
// MARK: Card Generation
152+
extension AnimatedCardsView {
153+
private func generateCards() {
154+
// Clear previous configuration
155+
if cardArray.count > 0 {
156+
_ = cardArray.map({ $0.removeFromSuperview() })
157+
}
158+
159+
cardArray = (0...cardCount).map { (tagId) in
160+
let view = generateNewCardViewWithTagId(tagId)
161+
self.addSubview(view)
162+
applyConstraintsToView(view)
163+
return view
164+
}
165+
}
166+
167+
private func generateNewCardViewWithTagId(tagId:NSInteger) -> UIView {
168+
let view = UIView()
169+
view.translatesAutoresizingMaskIntoConstraints = false
170+
view.tag = tagId+1
171+
switch tagId {
172+
case 0: view.backgroundColor = UIColor.purpleColor()
173+
case 1: view.backgroundColor = UIColor.redColor()
174+
case 2: view.backgroundColor = UIColor.blackColor()
175+
case 3: view.backgroundColor = UIColor.greenColor()
176+
case 4: view.backgroundColor = UIColor.brownColor()
177+
case 5: view.backgroundColor = UIColor.darkGrayColor()
178+
case 6: view.backgroundColor = UIColor.blueColor()
179+
case 7: view.backgroundColor = UIColor.orangeColor()
180+
default: view.backgroundColor = UIColor.whiteColor()
181+
}
182+
return view
183+
}
184+
185+
private func applyConstraintsToView(view:UIView) {
186+
view.addConstraints([
187+
NSLayoutConstraint(item: view, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: CGFloat(1.0), constant: Constants.DefaultSize.width),
188+
NSLayoutConstraint(item: view, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: Constants.DefaultSize.ratio, constant: 0),
189+
])
190+
view.superview!.addConstraints([
191+
NSLayoutConstraint(item: view, attribute: .CenterX, relatedBy: .Equal, toItem: view.superview, attribute: .CenterX, multiplier: CGFloat(1.0), constant: 0),
192+
NSLayoutConstraint(item: view, attribute: .CenterY, relatedBy: .Equal, toItem: view.superview, attribute: .CenterY, multiplier: CGFloat(1.0), constant: 0),
193+
])
194+
}
195+
}
196+
164197

165198
// MARK: Handle Layout
166199
extension AnimatedCardsView {

CardAnimation/ComponentViewController.swift

+19-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ComponentViewController: UIViewController {
1414

1515
override func viewDidLoad() {
1616
super.viewDidLoad()
17-
17+
cardsView.dataSourceDelegate = self
1818
// Do any additional setup after loading the view.
1919
}
2020

@@ -24,15 +24,7 @@ class ComponentViewController: UIViewController {
2424
}
2525

2626

27-
/*
28-
// MARK: - Navigation
29-
30-
// In a storyboard-based application, you will often want to do a little preparation before navigation
31-
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
32-
// Get the new view controller using segue.destinationViewController.
33-
// Pass the selected object to the new view controller.
34-
}
35-
*/
27+
// MARK: - Actions
3628
@IBAction func onUpPushed(sender: UIButton) {
3729
cardsView.flipUp()
3830
}
@@ -42,3 +34,20 @@ class ComponentViewController: UIViewController {
4234
}
4335

4436
}
37+
38+
// MARK: - AnimatedCardsViewDataSource
39+
extension ComponentViewController : AnimatedCardsViewDataSource {
40+
41+
func numberOfVisibleCards() -> Int {
42+
return 2
43+
}
44+
45+
func numberOfCards() -> Int {
46+
return 8
47+
}
48+
49+
func contentForCardNumber(number:Int, size:(width:CGFloat, height:CGFloat)) -> UIView {
50+
return UIView(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
51+
}
52+
53+
}

0 commit comments

Comments
 (0)