-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathCardContainerView.swift
736 lines (640 loc) · 28.9 KB
/
CardContainerView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//
// CardContainerView.swift
// CardAnimation
//
// Created by seedante on 16/1/19.
// Copyright © 2016 seedante
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import UIKit
/// The object adopt this protocol will provide a CardContainerView with card number and relative image.
public protocol CardContainerDataSource: class{
/// Return the number of cards in CardContainerView
///
/// - parameter cardContainerView: A CardContainerView object which ask for card number.
///
/// - returns: Card number. Usually, if returned value is negative, it means no card.
func numberOfCards(for cardContainerView: CardContainerView) -> Int
/// Return relative image at the specified location.
///
/// - parameter cardContainerView: A CardContainerView object which ask for image to display.
/// - parameter index: Card index in data source.
///
/// - returns: Image at the specified location.
func cardContainerView(_ cardContainerView: CardContainerView, imageForCardAt index: Int) -> UIImage?
}
private class _CardView: UIView {
private let backView = UIView()
private let imageView = UIImageView()
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.black
self.clipsToBounds = true
self.translatesAutoresizingMaskIntoConstraints = false
backView.translatesAutoresizingMaskIntoConstraints = false
backView.alpha = 0
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
addSubview(imageView)
insertSubview(backView, belowSubview: imageView)
addConstraint(NSLayoutConstraint(item: imageView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: imageView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: imageView, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: imageView, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: backView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: backView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: backView, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: backView, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0))
}
convenience init(frame: CGRect, backColor: UIColor) {
self.init(frame: frame)
backView.backgroundColor = backColor
}
convenience init(){
self.init(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 400, height: 300)))
}
required convenience init?(coder aDecoder: NSCoder) {
self.init(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 400, height: 300)))
}
func restoreTransform3D(){
layer.transform = CATransform3DIdentity
}
func setImage(_ image: UIImage?){
imageView.image = image
}
func hiddenCard(){
imageView.alpha = 0
}
func hiddenBack(){
backView.alpha = 0
}
func restoreBack(){
backView.alpha = 1
}
func restoreCard(){
imageView.alpha = 1
}
func setBrightness(_ value: CGFloat){
imageView.alpha = value
}
func setBorderColor(_ color: UIColor){
imageView.layer.borderColor = color.cgColor
}
func setBorderWidth(_ value: CGFloat){
imageView.layer.borderWidth = value
}
}
/// A view to display images like cards, support pan gesture to slide up and down.
/// Its prototype: https://cdn.dribbble.com/users/32399/screenshots/1265487/like-dribbble-video_2x.gif
open class CardContainerView: UIView {
// MARK: Properties to Configure
/// A Boolean value deciding whether control brightness on different cards. The default value is true.
public var enableBrightnessControl: Bool = true
/// The max number of visible cards in the view. The default value is 10.
public var maxVisibleCardCount: Int = 10
/// A Boolean value deciding whether provide a border on every card view. The default value is true.
public var needsBorder: Bool = true
/// The size of the first card you see. If you change this value, call `layoutCardsIfNeeded()` to resize cards.
/// The default value is (400, 300).
public var cardSize = CGSize(width: 400, height: 300)
/// Color of card's back. If it's nil, card back is black. The default value is nil.
public var cardBackColor: UIColor?
/// The border width of the first card you see. The default value is 5.
public var cardBorderWidth: CGFloat = 5
/// Specify max distance(points) between cards in vertical direction. The default value is 35.
public var maxYOffsetBetweenCards: CGFloat = 35
/// Specify min distance(points) between cards in vertical direction. The default value is 15.
public var minYOffsetBetweenCards: CGFloat = 15
/// The data source must adopt the CardContainerDataSource protocol. The data source is not retained.
public weak var dataSource: CardContainerDataSource?{
didSet{
if dataSource != nil{
configure()
}
}
}
// MARK: Query Card location
/// Head card's location in data source. If data source is nil or empty, returns nil.
/// Specially, if cards slide to end, it returns card count.
public var headCardIndexAtDataSource: Int?{
guard let cardCount = dataSource?.numberOfCards(for: self) else {
return nil
}
if cardCount <= 0{
return nil
}
return currentHeadCardIndex
}
var flipAnimationTime: TimeInterval = 0.4
// MARK: Init
/// Init a CardContainerView with specified frame and default card size.
///
/// - parameter frame: The frame rectangle for the view.
public override init(frame: CGRect) {
super.init(frame: frame)
clipsToBounds = true
translatesAutoresizingMaskIntoConstraints = false
panGesture.addTarget(self, action: #selector(CardContainerView.panGestureAction(_:)))
addGestureRecognizer(panGesture)
}
/// Init a CardContainerView with specified frame and card size.
///
/// - parameter frame: The frame rectangle for the view.
/// - parameter cardSize: The card size. The default value is (400, 300)
public init(frame: CGRect, cardSize: CGSize = CGSize(width: 400, height: 300)) {
self.cardSize = cardSize
super.init(frame: frame)
clipsToBounds = true
translatesAutoresizingMaskIntoConstraints = false
panGesture.addTarget(self, action: #selector(CardContainerView.panGestureAction(_:)))
addGestureRecognizer(panGesture)
}
/// Init from storyboard/xib file. Card size is default value: (400, 300).
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
clipsToBounds = true
translatesAutoresizingMaskIntoConstraints = false
panGesture.addTarget(self, action: #selector(CardContainerView.panGestureAction(_:)))
addGestureRecognizer(panGesture)
}
deinit{
self.removeGestureRecognizer(panGesture)
}
// MARK: Slide
private var fliping: Bool = false
private lazy var flipDownTransform3D: CATransform3D = {
var transform3D = CATransform3DIdentity
transform3D.m34 = -1.0 / 2000.0
transform3D = CATransform3DRotate(transform3D, CGFloat(-Double.pi), 1, 0, 0)
return transform3D
}()
/// Slide down the head card. This method is safe.
public func slideDown(){
guard let headCard = visibleCardQueue.first else{return}
guard !fliping else {return}
fliping = true
headCard.restoreBack()
UIView.animateKeyframes(withDuration: flipAnimationTime, delay: 0, options: UIViewKeyframeAnimationOptions(), animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1, animations: {
// Turn 180 degrees
headCard.layer.transform = self.flipDownTransform3D
})
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.01, animations: {
headCard.hiddenCard()
})
UIView.addKeyframe(withRelativeStartTime: 0.9, relativeDuration: 0.1, animations: {
headCard.alpha = 0
})
}, completion: {_ in
self.finishLastworkAfterSlideDown(headCard)
})
}
private func finishLastworkAfterSlideDown(_ headCard: _CardView){
headCard.layer.zPosition = CGFloat(101)
currentHeadCardIndex += 1
visibleCardQueue.removeFirst()
backupCardQueue.append(headCard)
if needsFillVisibleQueue == true{
fillVisibleQueue()
}
layoutVisibleCardViews()
}
/// Slide up a card to be the head card. The method is safe.
public func slideUp(){
guard let image = self.dataSource!.cardContainerView(self, imageForCardAt: self.currentHeadCardIndex - 1)else{
return
}
guard !fliping else {return}
previousHeadCard = candidateCardView
previousHeadCard?.setImage(image)
if isNewCard{
previousHeadCard?.layer.transform = flipDownTransform3D
previousHeadCard?.hiddenCard()
previousHeadCard?.alpha = 0
}
fliping = true
UIView.animateKeyframes(withDuration: flipAnimationTime, delay: 0, options: UIViewKeyframeAnimationOptions(), animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5, animations: {
self.previousHeadCard?.alpha = 1
})
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1, animations: {
self.previousHeadCard?.restoreTransform3D()
})
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.01, animations: {
self.previousHeadCard?.restoreCard()
})
}, completion: {_ in
self.previousHeadCard?.hiddenBack()
//There were some problems if the follow code in completion block ever
self.currentHeadCardIndex -= 1
self.visibleCardQueue.insert(self.previousHeadCard!, at: 0)
self.layoutVisibleCardViews()
})
}
// MARK: Update Cards
private func reloadCards(){
for (index, cardView) in visibleCardQueue.enumerated(){
let image = self.dataSource?.cardContainerView(self, imageForCardAt: index + currentHeadCardIndex)
cardView.setImage(image)
}
}
/// Insert a cark at specified location. You must update data source before calling this method.
/// And you must call this method after updating data source.
///
/// If location is not visible, no animation.
///
/// - parameter index: Card index in the data source.
public func insertCard(at index: Int){
if index < 0 || index > dataSource!.numberOfCards(for: self) - 1{
fatalError("Index \(index) is out of range.")
}
if index < currentHeadCardIndex{
currentHeadCardIndex += 1
return
}
let visibleIndex = index - currentHeadCardIndex
if visibleCardQueue.count > 0{
guard visibleCardQueue.count > visibleIndex else {return}
}else{
guard visibleIndex == 0 else{return}
}
let newCard = candidateCardView
if isNewCard{
newCard.alpha = 0
}else{
newCard.restoreTransform3D()
}
adjustConfigurationOfCardView(newCard, withTargetIndex: visibleIndex, yOffset: -cardSize.height/2)
visibleCardQueue.insert(newCard, at: visibleIndex)
reloadCards()
UIView.animate(withDuration: 0.25, animations: {
newCard.restoreCard()
newCard.alpha = 1
}, completion: {_ in
self.layoutVisibleCardViews(from: visibleIndex)
})
}
/// Remove card at specified location. You muust update data source before calling this method.
/// And you must call this method after updating data source.
///
/// If location is not visible, no animation.
///
/// - parameter index: Card index in the data source.
public func removeCard(at index: Int){
if index < 0 || index > dataSource!.numberOfCards(for: self){
fatalError("Index \(index) is out of range")
}
if index < currentHeadCardIndex{
currentHeadCardIndex -= 1
return
}
let visibleIndex = index - currentHeadCardIndex
guard visibleCardQueue.count > visibleIndex else {
return
}
let targetCard = visibleCardQueue[visibleIndex]
let offset = -cardSize.height
visibleCardQueue.remove(at: visibleIndex)
backupCardQueue.append(targetCard)
UIView.animateKeyframes(withDuration: 0.4, delay: 0, options: .beginFromCurrentState, animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1, animations: {
self.adjustConfigurationOfCardView(targetCard, withTargetIndex: visibleIndex, yOffset: offset)
self.layoutIfNeeded()
})
UIView.addKeyframe(withRelativeStartTime: 0.7, relativeDuration: 0.3, animations: {targetCard.alpha = 0})
}, completion: { _ in
self.resetToReuse(targetCard)
if self.needsFillVisibleQueue == true{
self.fillVisibleQueue()
self.reloadCards()
}
self.layoutVisibleCardViews()
})
}
/// Lay out all cards immediately. If you change `cardSize`, call this method to resize cards.
public func layoutCardsIfNeeded(){
layoutInvisibleCardViews()
layoutVisibleCardViews()
}
//MARK: Reuseable Card Queue
//An Array sorted from fisrt card to last card.
private var visibleCardQueue: [_CardView] = []
private var backupCardQueue: [_CardView] = []
private var currentHeadCardIndex: Int = 0
//MARK: Configure Method
private func configure(){
guard let cardCount = dataSource?.numberOfCards(for: self), cardCount > 0 else{return}
let vacancyCount = cardCount >= maxVisibleCardCount ? maxVisibleCardCount : cardCount
for _ in 0..<vacancyCount{
let newCard = candidateCardView
newCard.alpha = 1
visibleCardQueue.append(newCard)
}
reloadCards()
layoutVisibleCardViews()
}
private var isNewCard: Bool = true
private var candidateCardView: _CardView{
if let cardView = backupCardQueue.popLast(){
isNewCard = false
cardView.setImage(nil)
return cardView
}
let newCardView = generateNewCardView()
isNewCard = true
return newCardView
}
private func generateNewCardView() -> _CardView{
let newCardView: _CardView
if cardBackColor != nil{
newCardView = _CardView(frame: CGRect(origin: CGPoint.zero, size: cardSize), backColor: cardBackColor!)
}else{
newCardView = _CardView(frame: CGRect(origin: CGPoint.zero, size: cardSize))
}
newCardView.setBorderColor(UIColor.white)
// if not use AutoLayout, set frame directly, view will move after anchorPoint change.
// solution for using frame:
//
// let oldFrame = view.frame
// view.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
// view.frame = oldFrame
//
// No this problem with AutoLayout.
newCardView.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
addSubview(newCardView)
NSLayoutConstraint(item: newCardView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0).isActive = true
let offsetYConstraint = NSLayoutConstraint(item: newCardView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0 - cardSize.height/2)
let widthConstraint = NSLayoutConstraint(item: newCardView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0, constant: cardSize.width)
let heightConstraint = NSLayoutConstraint(item: newCardView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0, constant: cardSize.height)
offsetYConstraint.identifier = "offsetYConstraint"
widthConstraint.identifier = "WidthConstraint"
heightConstraint.identifier = "HeightConstraint"
offsetYConstraint.isActive = true
widthConstraint.isActive = true
heightConstraint.isActive = true
return newCardView
}
private func adjustConfigurationOfCardView(_ cardView: _CardView, withTargetIndex index: Int, yOffset: CGFloat = 0){
cardView.layer.zPosition = CGFloat(100 - index)
if needsBorder{
cardView.setBorderWidth(calculateScaleFactorForIndex(index) * cardBorderWidth)
}else{
cardView.setBorderWidth(0)
}
if enableBrightnessControl{
cardView.setBrightness(calculateAlphaForIndex(index))
}
let offsetYConstraint = self.constraints.filter({$0.identifier == "offsetYConstraint" && $0.firstItem as? _CardView === cardView}).first
offsetYConstraint?.constant = (0 - cardSize.height/2 - calculateYOffsetForIndex(index)) + yOffset
let scaleFactor: CGFloat = calculateScaleFactorForIndex(index)
let widthConstraint = cardView.constraints.filter({$0.identifier == "WidthConstraint"}).first
widthConstraint?.constant = cardSize.width * scaleFactor
let heightConstraint = cardView.constraints.filter({$0.identifier == "HeightConstraint"}).first
heightConstraint?.constant = cardSize.height * scaleFactor
}
private func layoutInvisibleCardViews(){
for cardView in backupCardQueue{
resetToReuse(cardView)
}
}
private func layoutVisibleCardViews(from startIndex: Int = 0){
UIView.animate(withDuration: 0.25, animations: {
for (index, cardView) in self.visibleCardQueue.enumerated(){
guard index >= startIndex else{continue}
if index >= self.maxVisibleCardCount{
cardView.alpha = 0
}else{
self.adjustConfigurationOfCardView(cardView, withTargetIndex: index)
}
}
self.layoutIfNeeded()
}, completion: {_ in
self.hiddenRedundantVisibleCardsIfNeeded()
self.fliping = false
})
}
private func hiddenRedundantVisibleCardsIfNeeded(){
if visibleCardQueue.count > maxVisibleCardCount{
let slice = visibleCardQueue[(maxVisibleCardCount ..< visibleCardQueue.count)]
for cardView in slice{
resetToReuse(cardView)
}
layoutIfNeeded()
backupCardQueue.append(contentsOf: slice)
visibleCardQueue.removeSubrange(maxVisibleCardCount..<visibleCardQueue.count)
}
}
private func resetToReuse(_ cardView: _CardView){
adjustConfigurationOfCardView(cardView, withTargetIndex: 0)
cardView.layer.zPosition = CGFloat(101)
if needsBorder{
cardView.setBorderWidth(cardBorderWidth)
}else{
cardView.setBorderWidth(0)
}
cardView.hiddenCard()
cardView.layer.transform = flipDownTransform3D
}
private var needsFillVisibleQueue: Bool{
if dataSource == nil{
return false
}else{
let cardCount = dataSource!.numberOfCards(for: self)
if visibleCardQueue.count < maxVisibleCardCount && currentHeadCardIndex + visibleCardQueue.count < cardCount{
return true
}
return false
}
}
private func fillVisibleQueue(){
if dataSource == nil{return}
let cardCount = dataSource!.numberOfCards(for: self)
let vacancyCount = currentHeadCardIndex + maxVisibleCardCount < cardCount ? maxVisibleCardCount - visibleCardQueue.count : cardCount - currentHeadCardIndex - visibleCardQueue.count
let startIndex = currentHeadCardIndex + visibleCardQueue.count
for index in 0..<vacancyCount{
let trailCard = self.candidateCardView
let image = self.dataSource?.cardContainerView(self, imageForCardAt: startIndex + index)
trailCard.setImage(image)
trailCard.alpha = 1
trailCard.restoreCard()
trailCard.restoreTransform3D()
self.adjustConfigurationOfCardView(trailCard, withTargetIndex: visibleCardQueue.count)
self.layoutIfNeeded()
self.visibleCardQueue.append(trailCard)
}
}
//MARK: Gesture Method
private let panGesture = UIPanGestureRecognizer()
private var isInitiallyDown: Bool = true
private var previousHeadCard: _CardView?
@objc private func panGestureAction(_ gesture: UIPanGestureRecognizer){
if dataSource == nil{return}
guard !fliping else {return}
let velocity = gesture.velocity(in: self)
let percent = gesture.translation(in: self).y/150
var flipTransform3D = CATransform3DIdentity
flipTransform3D.m34 = -1.0 / 2000.0
switch gesture.state{
case .began:
if velocity.y > 0{
isInitiallyDown = true
}else{
isInitiallyDown = false
if currentHeadCardIndex == 0{
return
}
previousHeadCard = candidateCardView
let image = self.dataSource?.cardContainerView(self, imageForCardAt: self.currentHeadCardIndex - 1)
previousHeadCard?.setImage(image)
previousHeadCard?.alpha = 1
}
case .changed:
if visibleCardQueue.count == 0 && isInitiallyDown == true{
return
}
if currentHeadCardIndex == 0 && isInitiallyDown == false{
return
}
if isInitiallyDown{
let headCard = visibleCardQueue.first
switch percent{
case 0.0..<1.0:
flipTransform3D = CATransform3DRotate(flipTransform3D, CGFloat(-Double.pi) * percent, 1, 0, 0)
headCard?.layer.transform = flipTransform3D
if percent >= 0.5{
headCard?.hiddenCard()
}else{
headCard?.restoreCard()
}
case 1.0...CGFloat(MAXFLOAT):
flipTransform3D = CATransform3DRotate(flipTransform3D, CGFloat(-Double.pi), 1, 0, 0)
headCard?.layer.transform = flipTransform3D
default: break
}
}else{
switch percent{
case -1.0...0:
flipTransform3D = CATransform3DRotate(flipTransform3D, CGFloat(-Double.pi) * (percent + 1.0), 1, 0, 0)
previousHeadCard?.layer.transform = flipTransform3D
if percent <= -0.5{
previousHeadCard?.restoreCard()
}else{
previousHeadCard?.hiddenCard()
}
default: break
}
}
case .ended, .cancelled:
if visibleCardQueue.count == 0 && isInitiallyDown == true{
return
}
if currentHeadCardIndex == 0 && isInitiallyDown == false{
return
}
fliping = true
if isInitiallyDown{
let headCard = visibleCardQueue.first
if percent >= 0.5{
UIView.animate(withDuration: 0.2, animations: {
headCard?.layer.transform = self.flipDownTransform3D
headCard?.alpha = 0
}, completion: {_ in
self.finishLastworkAfterSlideDown(headCard!)
})
}else{
headCard?.hiddenBack()
UIView.animate(withDuration: 0.2, animations: {
headCard?.restoreTransform3D()
}, completion: {_ in
self.fliping = false
})
}
}else{
if percent <= -0.5{
UIView.animate(withDuration: 0.2, animations: {
self.previousHeadCard?.restoreTransform3D()
}, completion: { _ in
self.currentHeadCardIndex -= 1
self.visibleCardQueue.insert(self.previousHeadCard!, at: 0)
self.layoutVisibleCardViews()
})
}else{
backupCardQueue.append(previousHeadCard!)
UIView.animate(withDuration: 0.2, animations: {
self.previousHeadCard?.layer.transform = self.flipDownTransform3D
self.previousHeadCard?.alpha = 0
}, completion: {_ in
self.fliping = false
})
}
}
default: break
}
}
//MARK: Calculation Helper Method
// y = k * x + m
private func calcuteResultWith(x1: Int, x2: Int, y1: CGFloat, y2: CGFloat, argument: Int) -> CGFloat{
if argument == x1{
return y1
}
if argument == x2{
return y2
}
let k = (y1-y2)/CGFloat(x1-x2)
let m = (CGFloat(x1) * y2 - CGFloat(x2) * y1)/CGFloat(x1-x2)
return k * CGFloat(argument) + m
}
// Scale for width, border width.
private func calculateScaleFactorForIndex(_ indexInQueue: Int) -> CGFloat{
if indexInQueue < 1{
return CGFloat(1)
}
var scale = calcuteResultWith(x1: 1, x2: 8, y1: 0.95, y2: 0.5, argument: indexInQueue)
if scale < 0.1{
scale = 0.1
}
return scale
}
var yOffsets: [CGFloat] = []
private func calculateYOffsetForIndex(_ indexInQueue: Int) -> CGFloat{
if indexInQueue < 1{
return CGFloat(0)
}
if yOffsets.count >= indexInQueue{
return yOffsets[..<indexInQueue].reduce(0, +)
}
let offset = calcuteResultWith(x1: 1, x2: maxVisibleCardCount, y1: maxYOffsetBetweenCards, y2: minYOffsetBetweenCards, argument: indexInQueue)
yOffsets.append(offset)
return yOffsets[..<indexInQueue].reduce(0, +)
}
private func calculateAlphaForIndex(_ indexInQueue: Int) -> CGFloat{
let alpha1Count: Int = 3
if indexInQueue < alpha1Count{
return CGFloat(1)
}
var alpha = calcuteResultWith(x1: alpha1Count, x2: 9, y1: 1, y2: 0.2, argument: indexInQueue)
if alpha < 0.1{
alpha = 0.1
}else if alpha > 1{
alpha = 1
}
return alpha
}
}