Skip to content

Instantly share code, notes, and snippets.

@apatronl
Last active June 20, 2020 18:14
Show Gist options
  • Save apatronl/222c43ebfa77be4d5bd2cccb8d56534a to your computer and use it in GitHub Desktop.
Save apatronl/222c43ebfa77be4d5bd2cccb8d56534a to your computer and use it in GitHub Desktop.
import UIKit
class HeartButton: UIButton {
private var isLiked = false
// ...
public func flipLikedState() {
isLiked = !isLiked
animate()
}
private func animate() {
// Step 1
UIView.animate(withDuration: 0.1, animations: {
let newImage = self.isLiked ? self.likedImage : self.unlikedImage
self.transform = self.transform.scaledBy(x: 0.8, y: 0.8)
self.setImage(newImage, for: .normal)
}, completion: { _ in
// Step 2
UIView.animate(withDuration: 0.1, animations: {
self.transform = CGAffineTransform.identity
})
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment