Created
December 15, 2020 14:18
-
-
Save anupamchugh/03b08b2f3dac25eb1a78f6e00987a425 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
struct DataModel { | |
var bgColor: UIColor | |
//Add your text or image properties | |
init(bgColor: UIColor) { | |
self.bgColor = bgColor | |
} | |
} | |
class StackContainerView: UIView, SwipeCardsDelegate { | |
//MARK: - Properties | |
var numberOfCardsToShow: Int = 0 | |
var cardsToBeVisible: Int = 3 | |
var cardViews : [TinderCardView] = [] | |
var remainingcards: Int = 0 | |
let horizontalInset: CGFloat = 10.0 | |
let verticalInset: CGFloat = 10.0 | |
var visibleCards: [TinderCardView] { | |
return subviews as? [TinderCardView] ?? [] | |
} | |
var dataSource: SwipeCardsDataSource? { | |
didSet { | |
reloadData() | |
} | |
} | |
//MARK: - Init | |
override init(frame: CGRect) { | |
super.init(frame: .zero) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
func reloadData() { | |
removeAllCardViews() | |
guard let datasource = dataSource else { return } | |
setNeedsLayout() | |
layoutIfNeeded() | |
numberOfCardsToShow = datasource.numberOfCardsToShow() | |
remainingcards = numberOfCardsToShow | |
for i in 0..<min(numberOfCardsToShow,cardsToBeVisible) { | |
addCardView(cardView: datasource.card(at: i), atIndex: i ) | |
} | |
} | |
//MARK: - Configurations | |
private func addCardView(cardView: TinderCardView, atIndex index: Int) { | |
cardView.delegate = self | |
addCardFrame(index: index, cardView: cardView) | |
cardViews.append(cardView) | |
insertSubview(cardView, at: 0) | |
remainingcards -= 1 | |
} | |
func addCardFrame(index: Int, cardView: TinderCardView) { | |
var cardViewFrame = bounds | |
let horizontalInset = (CGFloat(index) * self.horizontalInset) | |
let verticalInset = CGFloat(index) * self.verticalInset | |
cardViewFrame.size.width -= 2 * horizontalInset | |
cardViewFrame.origin.x += horizontalInset | |
cardViewFrame.origin.y += verticalInset | |
cardView.frame = cardViewFrame | |
} | |
private func removeAllCardViews() { | |
for cardView in visibleCards { | |
cardView.removeFromSuperview() | |
} | |
cardViews = [] | |
} | |
func swipeDidEnd(on view: TinderCardView) { | |
guard let datasource = dataSource else { return } | |
view.removeFromSuperview() | |
if remainingcards > 0 { | |
let newIndex = datasource.numberOfCardsToShow() - remainingcards | |
addCardView(cardView: datasource.card(at: newIndex), atIndex: 2) | |
for (cardIndex, cardView) in visibleCards.reversed().enumerated() { | |
UIView.animate(withDuration: 0.2, animations: { | |
cardView.center = self.center | |
self.addCardFrame(index: cardIndex, cardView: cardView) | |
self.layoutIfNeeded() | |
}) | |
} | |
}else { | |
for (cardIndex, cardView) in visibleCards.reversed().enumerated() { | |
UIView.animate(withDuration: 0.2, animations: { | |
cardView.center = self.center | |
self.addCardFrame(index: cardIndex, cardView: cardView) | |
self.layoutIfNeeded() | |
}) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment