8
8
9
9
import UIKit
10
10
11
+ protocol SDECardSource {
12
+ var cardCount : Int { get set }
13
+ func cardImageAtIndex( index: Int ) -> UIImage ?
14
+ }
15
+
11
16
enum panScrollDirection {
12
17
case Up, Down
13
18
}
14
19
20
+ enum JusticeLeagueHeroLogo : String {
21
+ case WonderWoman = " wonder_woman_logo_by_kalangozilla.jpg "
22
+ case Superman = " superman_kingdom_come_logo_by_kalangozilla.jpg "
23
+ case Batman = " batman_begins_poster_style_logo_by_kalangozilla.jpg "
24
+ case GreenLantern = " green_lantern_corps_logo_by_kalangozilla.jpg "
25
+ case Flash = " flash_logo_by_kalangozilla.jpg "
26
+ case Aquaman = " aquaman_young_justice_logo_by_kalangozilla.jpg "
27
+ case CaptainMarvel = " classic_captain_marvel_jr_logo_by_kalangozilla.jpg "
28
+ //can't find Cybord's Logo.
29
+ case AllMembers = " JLAFRICA.jpeg "
30
+ }
31
+
32
+
15
33
class ViewController : UIViewController {
16
34
17
35
var frontCardTag = 1
18
- var cardCount = 8
36
+ var cardCount = 0
37
+ let maxVisibleCardCount = 8
19
38
let gradientBackgroundLayer = CAGradientLayer ( )
20
- var originFrame = CGRectZero
21
39
var gestureDirection : panScrollDirection = . Up
40
+ var logoArray : [ JusticeLeagueHeroLogo ] = [ . Superman, . WonderWoman, . Batman, . GreenLantern, . Flash, . Aquaman, . CaptainMarvel, . AllMembers] {
41
+ didSet{
42
+ cardCount = logoArray. count
43
+ }
44
+ }
22
45
23
46
@IBOutlet weak var frontCenterYConstraint : NSLayoutConstraint !
24
47
48
+ //MARK: View Life Management
25
49
override func viewDidLoad( ) {
26
50
super. viewDidLoad ( )
27
51
@@ -32,6 +56,7 @@ class ViewController: UIViewController {
32
56
let scrollGesture = UIPanGestureRecognizer ( target: self , action: " scrollOnView: " )
33
57
view. addGestureRecognizer ( scrollGesture)
34
58
59
+ cardCount = logoArray. count
35
60
relayoutSubViews ( )
36
61
}
37
62
@@ -40,7 +65,56 @@ class ViewController: UIViewController {
40
65
// Dispose of any resources that can be recreated.
41
66
}
42
67
68
+ //MARK: Data Source
69
+ func cardImageAtIndex( index: Int ) -> UIImage ? {
70
+ return UIImage ( named: logoArray [ index] . rawValue) !
71
+ }
72
+
43
73
//MARK: Action Method
74
+ @IBAction func addCard( sender: AnyObject ) {
75
+ let newCardView = createNewCardViewWith ( UIImage ( named: JusticeLeagueHeroLogo . Batman. rawValue) )
76
+ view. addSubview ( newCardView)
77
+
78
+ logoArray. append ( . Batman)
79
+ let YOffset = 0 - calculusYOffsetForIndex( logoArray. count)
80
+ let widthConstraint = calculateWidthScaleForIndex ( logoArray. count) * view. bounds. size. width
81
+ let borderWidth = widthConstraint/ 100
82
+ newCardView. layer. borderColor = UIColor . whiteColor ( ) . CGColor
83
+ newCardView. layer. borderWidth = borderWidth
84
+ //添加layout constraint 必须在 addSubView() 后执行
85
+ NSLayoutConstraint ( item: newCardView, attribute: . CenterX, relatedBy: . Equal, toItem: view, attribute: . CenterX, multiplier: 1 , constant: 0 ) . active = true
86
+ NSLayoutConstraint ( item: newCardView, attribute: . CenterY, relatedBy: . Equal, toItem: view, attribute: . CenterY, multiplier: 1 , constant: YOffset) . active = true
87
+ NSLayoutConstraint ( item: newCardView, attribute: . Width, relatedBy: . Equal, toItem: nil , attribute: . NotAnAttribute, multiplier: 1 , constant: widthConstraint) . active = true
88
+ NSLayoutConstraint ( item: newCardView, attribute: . Width, relatedBy: . Equal, toItem: newCardView, attribute: . Height, multiplier: 4.0 / 3.0 , constant: 0 ) . active = true
89
+
90
+ }
91
+
92
+ func createNewCardViewWith( image: UIImage ? ) -> UIView {
93
+ let newCardView = UIView ( )
94
+ newCardView. translatesAutoresizingMaskIntoConstraints = false
95
+ newCardView. backgroundColor = UIColor . brownColor ( )
96
+ newCardView. tag = logoArray. count + 1
97
+ newCardView. clipsToBounds = true
98
+ newCardView. alpha = calculateAlphaForIndex ( logoArray. count + 1 - frontCardTag)
99
+ newCardView. layer. zPosition = CGFloat ( 1000 - logoArray. count - 1 + frontCardTag)
100
+
101
+
102
+ let subImageView = UIImageView ( image: image)
103
+ subImageView. translatesAutoresizingMaskIntoConstraints = false
104
+ subImageView. contentMode = . ScaleAspectFill
105
+ subImageView. clipsToBounds = true
106
+ subImageView. tag = 10
107
+
108
+ newCardView. addSubview ( subImageView)
109
+ NSLayoutConstraint ( item: subImageView, attribute: . CenterX, relatedBy: . Equal, toItem: newCardView, attribute: . CenterX, multiplier: 1 , constant: 0 ) . active = true
110
+ NSLayoutConstraint ( item: subImageView, attribute: . CenterY, relatedBy: . Equal, toItem: newCardView, attribute: . CenterY, multiplier: 1 , constant: 0 ) . active = true
111
+ NSLayoutConstraint ( item: subImageView, attribute: . Width, relatedBy: . Equal, toItem: newCardView, attribute: . Width, multiplier: 1 , constant: 0 ) . active = true
112
+ NSLayoutConstraint ( item: subImageView, attribute: . Height, relatedBy: . Equal, toItem: newCardView, attribute: . Height, multiplier: 1 , constant: 0 ) . active = true
113
+
114
+ return newCardView
115
+ }
116
+
117
+
44
118
@IBAction func flipUp( sender: AnyObject ) {
45
119
if frontCardTag == 1 {
46
120
return
@@ -246,8 +320,13 @@ class ViewController: UIViewController {
246
320
func relayoutSubViewWith( viewTag: Int , relativeIndex: Int , delay: NSTimeInterval , haveBorderWidth: Bool ) {
247
321
let width = view. bounds. size. width
248
322
if let subView = view. viewWithTag ( viewTag) {
249
- let alpha = calculateAlphaForIndex ( relativeIndex)
250
- subView. alpha = alpha
323
+
324
+ if let nestedImageView = subView. viewWithTag ( 10 ) as? UIImageView {
325
+ nestedImageView. image = cardImageAtIndex ( viewTag - 1 )
326
+ }
327
+
328
+ subView. layer. zPosition = CGFloat ( 1000 - relativeIndex)
329
+ subView. alpha = calculateAlphaForIndex ( relativeIndex)
251
330
252
331
var borderWidth : CGFloat = 0
253
332
let filterSubViewConstraints = subView. constraints. filter ( { $0. firstAttribute == . Width && $0. secondItem == nil } )
@@ -281,17 +360,18 @@ class ViewController: UIViewController {
281
360
282
361
func adjustUpViewLayout( ) {
283
362
if frontCardTag >= 2 {
363
+ let endCardTag = cardCount - frontCardTag > maxVisibleCardCount - 1 ? ( frontCardTag + maxVisibleCardCount - 1 ) : cardCount
284
364
let feed : UInt32 = 2
285
365
let randomRoll = arc4random_uniform ( feed)
286
366
switch randomRoll{
287
367
case 0 :
288
- for var viewTag = frontCardTag; viewTag <= cardCount ; ++ viewTag{
368
+ for var viewTag = frontCardTag; viewTag <= endCardTag ; ++ viewTag{
289
369
let delay : NSTimeInterval = Double ( viewTag - frontCardTag) * 0.1
290
370
let relativeIndex = viewTag - frontCardTag + 1
291
371
relayoutSubViewWith ( viewTag, relativeIndex: relativeIndex, delay: delay, haveBorderWidth: true )
292
372
}
293
373
case 1 :
294
- for var viewTag = cardCount ; viewTag >= frontCardTag; -- viewTag{
374
+ for var viewTag = endCardTag ; viewTag >= frontCardTag; -- viewTag{
295
375
let delay : NSTimeInterval = Double ( cardCount - viewTag) * 0.1
296
376
let relativeIndex = viewTag - frontCardTag + 1
297
377
relayoutSubViewWith ( viewTag, relativeIndex: relativeIndex, delay: delay, haveBorderWidth: true )
@@ -306,9 +386,9 @@ class ViewController: UIViewController {
306
386
307
387
func adjustDownViewLayout( ) {
308
388
frontCardTag += 1
309
-
310
- if frontCardTag <= cardCount {
311
- for viewTag in frontCardTag... cardCount {
389
+ let endCardTag = cardCount - frontCardTag > maxVisibleCardCount - 1 ? ( frontCardTag + maxVisibleCardCount - 1 ) : cardCount
390
+ if frontCardTag <= endCardTag {
391
+ for viewTag in frontCardTag... endCardTag {
312
392
let delay : NSTimeInterval = 0.1 * Double( viewTag - frontCardTag)
313
393
let relativeIndex = viewTag - frontCardTag
314
394
relayoutSubViewWith ( viewTag, relativeIndex: relativeIndex, delay: delay, haveBorderWidth: true )
@@ -317,8 +397,9 @@ class ViewController: UIViewController {
317
397
}
318
398
319
399
func relayoutSubViews( ) {
320
- if frontCardTag <= cardCount{
321
- for viewTag in frontCardTag... cardCount{
400
+ let endCardTag = cardCount - frontCardTag > maxVisibleCardCount - 1 ? ( frontCardTag + maxVisibleCardCount - 1 ) : cardCount
401
+ if frontCardTag <= endCardTag{
402
+ for viewTag in frontCardTag... endCardTag{
322
403
if let subView = view. viewWithTag ( viewTag) {
323
404
subView. layer. anchorPoint = CGPointMake ( 0.5 , 1 )
324
405
let relativeIndex = viewTag - frontCardTag
@@ -345,25 +426,6 @@ class ViewController: UIViewController {
345
426
}
346
427
347
428
//MARK: Helper Method
348
- func setAnchorPoint( anchorPoint: CGPoint , forView view: UIView ) {
349
- var newPoint = CGPointMake ( view. bounds. size. width * anchorPoint. x, view. bounds. size. height * anchorPoint. y)
350
- var oldPoint = CGPointMake ( view. bounds. size. width * view. layer. anchorPoint. x, view. bounds. size. height * view. layer. anchorPoint. y)
351
-
352
- newPoint = CGPointApplyAffineTransform ( newPoint, view. transform)
353
- oldPoint = CGPointApplyAffineTransform ( oldPoint, view. transform)
354
-
355
- var position = view. layer. position
356
- position. x -= oldPoint. x
357
- position. x += newPoint. x
358
-
359
- position. y -= oldPoint. y
360
- position. y += newPoint. y
361
-
362
- view. layer. position = position
363
- view. layer. anchorPoint = anchorPoint
364
- }
365
-
366
-
367
429
//f(x) = k * x + m
368
430
func calculateFactorOfFunction( x1: CGFloat , x2: CGFloat , y1: CGFloat , y2: CGFloat ) -> ( CGFloat , CGFloat ) {
369
431
0 commit comments