9
9
import UIKit
10
10
11
11
public protocol AnimatedCardsViewDataSource : class {
12
-
12
+ func numberOfVisibleCards( ) -> Int
13
+ func numberOfCards( ) -> Int
14
+ func contentForCardNumber( number: Int , size: ( width: CGFloat , height: CGFloat ) ) -> UIView
13
15
}
14
16
15
17
public class AnimatedCardsView : UIView {
16
18
17
19
private var cardArray : [ UIView ] !
20
+ private lazy var gestureRecognizer : UIPanGestureRecognizer = {
21
+ return UIPanGestureRecognizer ( target: self , action: " scrollOnView: " )
22
+ } ( )
18
23
19
- public weak var dataSource : AnimatedCardsViewDataSource ?
24
+ public weak var dataSourceDelegate : AnimatedCardsViewDataSource ? {
25
+ didSet {
26
+ if dataSourceDelegate != nil {
27
+ configure ( )
28
+ }
29
+ }
30
+ }
20
31
21
32
public struct Constants {
22
33
struct DefaultSize {
23
34
static let width : CGFloat = 400.0
24
35
static let ratio : CGFloat = 3.0 / 4.0
25
36
}
26
- static let numberOfCards = 8
37
+ }
38
+
39
+ private struct PrivateConstants {
40
+ static let maxVisibleCardCount = 8
41
+ static let cardCount = 8
27
42
}
28
43
29
44
var frontCardTag = 1
30
- var cardCount = 0
31
- let maxVisibleCardCount = 8
45
+ var cardCount = PrivateConstants . cardCount
46
+ var maxVisibleCardCount = PrivateConstants . maxVisibleCardCount
32
47
let gradientBackgroundLayer = CAGradientLayer ( )
33
48
var gestureDirection : panScrollDirection = . Up
34
49
50
+
51
+ // MARK: Initializers
35
52
override init ( frame: CGRect ) {
36
53
cardArray = [ ]
37
54
super. init ( frame: frame)
@@ -45,53 +62,23 @@ public class AnimatedCardsView: UIView {
45
62
configure ( )
46
63
}
47
64
65
+ // MARK: Config
48
66
private func configure( ) {
49
67
generateCards ( )
50
- let scrollGesture = UIPanGestureRecognizer ( target: self , action: " scrollOnView: " )
51
- self . addGestureRecognizer ( scrollGesture)
52
- cardCount = Constants . numberOfCards
68
+ configureConstants ( )
69
+ addGestureRecognizer ( gestureRecognizer)
53
70
relayoutSubViews ( )
54
71
}
55
72
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
66
76
}
67
77
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
85
79
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 ( )
95
82
}
96
83
97
84
public func flipUp( ) {
@@ -161,6 +148,52 @@ public class AnimatedCardsView: UIView {
161
148
162
149
}
163
150
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
+
164
197
165
198
// MARK: Handle Layout
166
199
extension AnimatedCardsView {
0 commit comments