Add card wiggling animation loop

This commit is contained in:
CypherPoet 2019-03-07 04:26:25 -05:00
parent bf9547d804
commit 737a24e927
2 changed files with 55 additions and 8 deletions

View File

@ -35,13 +35,8 @@ class CardViewController: UIViewController {
})
}
// MARK: - Methods
// MARK: - Event handling
@objc func cardTapped(_ sender: UIGestureRecognizer) {
delegate.cardTapped(self)
}
@objc func flipToReveal() {
UIView.transition(
with: view,
@ -62,6 +57,34 @@ class CardViewController: UIViewController {
})
}
@objc func wiggle() {
UIView.animate(
withDuration: 0.25,
delay: 0,
options: [.allowUserInteraction],
animations: { [weak self] in
self?.backImageView.transform = CGAffineTransform(
scaleX: 1.04, y: 1.04
).concatenating(
CGAffineTransform(rotationAngle: (6.6 / 180.0) * .pi)
).concatenating(
CGAffineTransform(rotationAngle: -(6.6 / 180.0) * .pi)
)
},
completion: { [weak self] _ in
self?.backImageView.transform = CGAffineTransform.identity
}
)
}
// MARK: - Event handling
@objc func cardTapped(_ sender: UIGestureRecognizer) {
delegate.cardTapped(self)
}
/*
// MARK: - Navigation

View File

@ -35,6 +35,10 @@ class HomeViewController: UIViewController {
setupBackground()
loadCards()
setupWiggling()
playBackgroundMusic()
currentCardState = .allFlat
}
@ -67,8 +71,6 @@ class HomeViewController: UIViewController {
cardViewControllers.append(cardViewController)
}
currentCardState = .allFlat
}
@ -118,6 +120,28 @@ class HomeViewController: UIViewController {
}
func setupWiggling() {
cardViewControllers.forEach({ card in
perform(#selector(wiggle), with: card, afterDelay: Double.random(in: 0...10))
})
}
@objc func wiggle(_ card: CardViewController) {
card.wiggle()
if Int.random(in: 0...1) == 1 {
perform(#selector(wiggle), with: card, afterDelay: Double.random(in: 1...2))
} else {
perform(#selector(wiggle), with: card, afterDelay: Double.random(in: 7...10))
}
}
func playBackgroundMusic() {
}
// MARK: - Private functions
private func makeCardPositions() -> [CGPoint] {