Skip to content

Commit 8c26e61

Browse files
committed
Externalize card creation and create 2 base cards to customize.
1 parent 5807a23 commit 8c26e61

File tree

4 files changed

+72
-25
lines changed

4 files changed

+72
-25
lines changed

CardAnimation.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
D21FB6791BCED32B0021B734 /* ComponentViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D21FB6781BCED32B0021B734 /* ComponentViewController.swift */; settings = {ASSET_TAGS = (); }; };
1111
D21FB67C1BCED4C20021B734 /* AnimatedCardsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D21FB67B1BCED4C20021B734 /* AnimatedCardsView.swift */; settings = {ASSET_TAGS = (); }; };
12+
D21FC1301BD0730D004D033D /* CardViews.swift in Sources */ = {isa = PBXBuildFile; fileRef = D21FC12F1BD0730D004D033D /* CardViews.swift */; settings = {ASSET_TAGS = (); }; };
1213
FDD71FB91BBB360A00E076EE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDD71FB81BBB360A00E076EE /* AppDelegate.swift */; };
1314
FDD71FBB1BBB360A00E076EE /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDD71FBA1BBB360A00E076EE /* ViewController.swift */; };
1415
FDD71FBE1BBB360A00E076EE /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FDD71FBC1BBB360A00E076EE /* Main.storyboard */; };
@@ -47,6 +48,7 @@
4748
/* Begin PBXFileReference section */
4849
D21FB6781BCED32B0021B734 /* ComponentViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ComponentViewController.swift; sourceTree = "<group>"; };
4950
D21FB67B1BCED4C20021B734 /* AnimatedCardsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AnimatedCardsView.swift; path = AnimatedCardView/AnimatedCardsView.swift; sourceTree = "<group>"; };
51+
D21FC12F1BD0730D004D033D /* CardViews.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CardViews.swift; sourceTree = "<group>"; };
5052
FDD71FB51BBB360A00E076EE /* CardAnimation.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CardAnimation.app; sourceTree = BUILT_PRODUCTS_DIR; };
5153
FDD71FB81BBB360A00E076EE /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
5254
FDD71FBA1BBB360A00E076EE /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
@@ -100,6 +102,7 @@
100102
isa = PBXGroup;
101103
children = (
102104
D21FB67B1BCED4C20021B734 /* AnimatedCardsView.swift */,
105+
D21FC12F1BD0730D004D033D /* CardViews.swift */,
103106
);
104107
name = Component;
105108
sourceTree = "<group>";
@@ -309,6 +312,7 @@
309312
D21FB67C1BCED4C20021B734 /* AnimatedCardsView.swift in Sources */,
310313
FDD71FB91BBB360A00E076EE /* AppDelegate.swift in Sources */,
311314
D21FB6791BCED32B0021B734 /* ComponentViewController.swift in Sources */,
315+
D21FC1301BD0730D004D033D /* CardViews.swift in Sources */,
312316
);
313317
runOnlyForDeploymentPostprocessing = 0;
314318
};

CardAnimation/AnimatedCardView/AnimatedCardsView.swift

+9-23
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import UIKit
1111
public protocol AnimatedCardsViewDataSource : class {
1212
func numberOfVisibleCards() -> Int
1313
func numberOfCards() -> Int
14-
func contentForCardNumber(number:Int, size:(width:CGFloat, height:CGFloat)) -> UIView
14+
func cardNumber(number:Int) -> BaseCardView
1515
}
1616

1717
public class AnimatedCardsView: UIView {
@@ -71,13 +71,11 @@ public class AnimatedCardsView: UIView {
7171
// MARK: Initializers
7272
override init(frame: CGRect) {
7373
super.init(frame: frame)
74-
configure()
7574
}
7675

7776
required public init?(coder aDecoder: NSCoder) {
7877
super.init(coder: aDecoder)
7978
backgroundColor = UIColor.yellowColor()
80-
configure()
8179
}
8280

8381
// MARK: Config
@@ -127,9 +125,9 @@ public class AnimatedCardsView: UIView {
127125
currentIndex++
128126

129127
let frontView = cardArray.removeFirst()
130-
131-
if currentIndex + cardArray.count < cardCount {
132-
addNewCardViewWithIndex(currentIndex, insertOnRear: true)
128+
let lastIndex = currentIndex + cardArray.count
129+
if lastIndex < cardCount {
130+
addNewCardViewWithIndex(lastIndex, insertOnRear: true)
133131
}
134132

135133
UIView.animateWithDuration(animationsSpeed*1.5, animations: {
@@ -163,8 +161,8 @@ extension AnimatedCardsView {
163161
}
164162
}
165163

166-
cardArray = (0..<maxVisibleCardCount).map { (tagId) in
167-
let view = generateNewCardViewWithTagId(tagId)
164+
cardArray = (0..<maxVisibleCardCount).map { (index) in
165+
let view = generateNewCardViewWithIndex(index)
168166
addSubview(view)
169167
applyConstraintsToView(view)
170168
return view
@@ -173,7 +171,7 @@ extension AnimatedCardsView {
173171

174172
private func addNewCardViewWithIndex(index:Int, insertOnRear rear:Bool = false) -> UIView {
175173
let newIndex = rear ? subviews.count : 0
176-
let newView = generateNewCardViewWithTagId(index)
174+
let newView = generateNewCardViewWithIndex(index)
177175
rear ? insertSubview(newView, atIndex: newIndex) : addSubview(newView)
178176
rear ? cardArray.append(newView) : cardArray.insert(newView, atIndex: newIndex)
179177
applyConstraintsToView(newView)
@@ -182,21 +180,9 @@ extension AnimatedCardsView {
182180
return newView
183181
}
184182

185-
private func generateNewCardViewWithTagId(tagId:NSInteger) -> UIView {
186-
let view = UIView()
183+
private func generateNewCardViewWithIndex(index:Int) -> UIView {
184+
let view = self.dataSourceDelegate!.cardNumber(index)
187185
view.translatesAutoresizingMaskIntoConstraints = false
188-
view.tag = tagId+1
189-
switch tagId {
190-
case 0: view.backgroundColor = UIColor.purpleColor()
191-
case 1: view.backgroundColor = UIColor.redColor()
192-
case 2: view.backgroundColor = UIColor.blackColor()
193-
case 3: view.backgroundColor = UIColor.greenColor()
194-
case 4: view.backgroundColor = UIColor.brownColor()
195-
case 5: view.backgroundColor = UIColor.darkGrayColor()
196-
case 6: view.backgroundColor = UIColor.blueColor()
197-
case 7: view.backgroundColor = UIColor.orangeColor()
198-
default: view.backgroundColor = UIColor.whiteColor()
199-
}
200186
return view
201187
}
202188

CardAnimation/CardViews.swift

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// CardView.swift
3+
// CardAnimation
4+
//
5+
// Created by Luis Sanchez Garcia on 16/10/15.
6+
// Copyright © 2015 seedante. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
public class BaseCardView: UIView {
12+
13+
}
14+
15+
class ImageCardView: BaseCardView {
16+
var imageView:UIImageView!
17+
override init(frame: CGRect) {
18+
super.init(frame: frame)
19+
configure()
20+
}
21+
22+
required init?(coder aDecoder: NSCoder) {
23+
super.init(coder: aDecoder)
24+
configure()
25+
}
26+
27+
private func configure() {
28+
imageView = UIImageView.init(frame: frame)
29+
imageView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
30+
imageView.backgroundColor = UIColor.lightGrayColor()
31+
imageView.clipsToBounds = true
32+
imageView.contentMode = .ScaleAspectFill
33+
addSubview(imageView)
34+
}
35+
}

CardAnimation/ComponentViewController.swift

+24-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@ class ComponentViewController: UIViewController {
3939

4040
}
4141

42+
enum JusticeLeagueLogos: String {
43+
case WonderWoman = "wonder_woman_logo_by_kalangozilla.jpg"
44+
case Superman = "superman_kingdom_come_logo_by_kalangozilla.jpg"
45+
case Batman = "batman_begins_poster_style_logo_by_kalangozilla.jpg"
46+
case GreenLantern = "green_lantern_corps_logo_by_kalangozilla.jpg"
47+
case Flash = "flash_logo_by_kalangozilla.jpg"
48+
case Aquaman = "aquaman_young_justice_logo_by_kalangozilla.jpg"
49+
case CaptainMarvel = "classic_captain_marvel_jr_logo_by_kalangozilla.jpg"
50+
//can't find Cybord's Logo.
51+
case AllMembers = "JLAFRICA.jpeg"
52+
53+
static var logoArray : [JusticeLeagueLogos] {
54+
get {
55+
return [.Superman, .WonderWoman, .Batman, .GreenLantern, .Flash, .Aquaman, .CaptainMarvel, .AllMembers]
56+
}
57+
}
58+
}
59+
60+
4261
// MARK: - AnimatedCardsViewDataSource
4362
extension ComponentViewController : AnimatedCardsViewDataSource {
4463

@@ -50,8 +69,11 @@ extension ComponentViewController : AnimatedCardsViewDataSource {
5069
return 8
5170
}
5271

53-
func contentForCardNumber(number:Int, size:(width:CGFloat, height:CGFloat)) -> UIView {
54-
return UIView(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
72+
func cardNumber(number:Int) -> BaseCardView {
73+
print(" 🃏 Requested card number \(number)")
74+
let view = ImageCardView(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
75+
view.imageView.image = UIImage.init(named: JusticeLeagueLogos.logoArray[number].rawValue)
76+
return view
5577
}
5678

5779
}

0 commit comments

Comments
 (0)