functionality for animating cards on tap

This commit is contained in:
CypherPoet 2019-03-06 01:23:50 -05:00
parent cd99ccda68
commit 5d3546a137
2 changed files with 60 additions and 1 deletions

View File

@ -11,7 +11,7 @@ import UIKit
class CardViewController: UIViewController {
// MARK: - Instance Properties
weak var delegate: UIViewController!
weak var delegate: HomeViewController!
lazy var frontImageView = makeFrontImageView()
lazy var backImageView = makeBackImageView()
@ -35,6 +35,33 @@ class CardViewController: UIViewController {
})
}
// MARK: - Event handling
@objc func cardTapped(_ sender: UIGestureRecognizer) {
delegate.cardTapped(self)
}
@objc func flipToReveal() {
UIView.transition(
with: view,
duration: 0.7,
options: [.transitionFlipFromRight],
animations: { [weak self] in
self?.backImageView.isHidden = true
self?.frontImageView.isHidden = false
}
)
}
@objc func fadeAway() {
UIView.animate(withDuration: 0.7, animations: { [weak self] in
self?.view.transform = CGAffineTransform(scaleX: 0.00001, y: 0.00001) // shrink down
self?.view.alpha = 0
})
}
/*
// MARK: - Navigation
@ -62,6 +89,8 @@ class CardViewController: UIViewController {
let imageView = makeCardImageView()
imageView.alpha = 0
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(cardTapped)))
return imageView
}

View File

@ -11,8 +11,15 @@ import UIKit
class HomeViewController: UIViewController {
@IBOutlet var cardContainer: UIView!
enum CardState {
case allFlat
case flipping
}
// MARK: - Instance Properties
var currentCardState = CardState.allFlat
var cardViewControllers: [CardViewController] = []
lazy var cardPositions = makeCardPositions()
@ -58,6 +65,8 @@ class HomeViewController: UIViewController {
cardViewControllers.append(cardViewController)
}
currentCardState = .allFlat
}
func removeCardsInView() {
@ -69,6 +78,27 @@ class HomeViewController: UIViewController {
cardViewControllers.removeAll(keepingCapacity: true)
}
// MARK: - Event handling
func cardTapped(_ tappedCard: CardViewController) {
guard currentCardState == .allFlat else { return }
currentCardState = .flipping
for card in cardViewControllers {
if card == tappedCard {
card.flipToReveal()
card.perform(#selector(card.fadeAway), with: nil, afterDelay: 1)
} else {
card.fadeAway()
}
}
perform(#selector(loadCards), with: nil, afterDelay: 2)
}
// MARK: - Private functions
private func makeCardPositions() -> [CGPoint] {