Last active
April 25, 2024 15:17
-
-
Save clarkeben/4d423cd9e7346b5fa277a358907f2bd2 to your computer and use it in GitHub Desktop.
A list of simple yet effective animation extensions for UIKit
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 | |
//MARK: - UIButton Animations | |
extension UIButton { | |
// Contract and retract animation | |
func contractRetractBtn(duration: Double) { | |
let contraction = CASpringAnimation(keyPath: "transform.scale") | |
contraction.duration = duration | |
contraction.fromValue = 0.96 | |
contraction.toValue = 1.0 | |
contraction.autoreverses = true | |
contraction.repeatCount = 3 | |
contraction.initialVelocity = 0.5 | |
contraction.damping = 1.0 | |
layer.add(contraction, forKey: nil) | |
} | |
// Flash animation | |
func flashBtn(duration: Double) { | |
let flash = CABasicAnimation(keyPath: "opacity") | |
flash.duration = duration | |
flash.fromValue = 1 | |
flash.toValue = 0.1 | |
flash.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut) | |
flash.autoreverses = true | |
flash.repeatCount = 3 | |
layer.add(flash, forKey: nil) | |
} | |
// A simple fade in animation | |
func fadeInBtn(duration: Double) { | |
self.alpha = 0 | |
UIView.animate(withDuration: duration) { | |
self.alpha = 1.0 | |
} | |
} | |
} | |
//MARK: - UILabel Animations | |
extension UILabel { | |
// Typing animation | |
func typingTextAnimation(text: String, timeInterval: Double) { | |
self.text = "" | |
self.alpha = 0 | |
var charIndex = 0.0 | |
for letter in text { | |
Timer.scheduledTimer(withTimeInterval: timeInterval * charIndex, repeats: false) { (timer) in | |
self.text?.append(letter) | |
} | |
charIndex += 1 | |
} | |
UIView.animate(withDuration: 1.0) { | |
self.alpha = 1.0 | |
} | |
} | |
} | |
//MARK: - UItableView Animations | |
extension UITableView { | |
// Animates cells moving them from bottom to top with a spring bounce animation | |
func reloadWithBounceAnimation() { | |
self.reloadData() | |
let tableViewHeight = self.bounds.size.height | |
let cells = self.visibleCells | |
var delayCounter = 0 | |
for cell in cells { | |
cell.transform = CGAffineTransform(translationX: 0, y: tableViewHeight) | |
} | |
for cell in cells { | |
UIView.animate(withDuration: 1.6, delay: 0.08 * Double(delayCounter),usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: .curveEaseInOut, animations: { | |
cell.transform = CGAffineTransform.identity | |
}, completion: nil) | |
delayCounter += 1 | |
} | |
} | |
// Animate cells loading from left to right | |
func reloadLeftToRight() { | |
self.reloadData() | |
let cells = visibleCells | |
var delayCounter = 0 | |
for cell in cells { | |
cell.frame.origin.x = -cell.frame.width | |
UIView.animate(withDuration: 0.7, delay: 0.06 * Double(delayCounter), usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: { | |
cell.frame.origin.x = 0 | |
}, completion: nil) | |
delayCounter += 1 | |
} | |
} | |
} | |
//MARK: - UITableViewCell Animations | |
extension UITableViewCell { | |
// Animates cells to fly from left to right as user scrolls | |
func flyInCell(duration: Double) { | |
let rotationTransform = CATransform3DTranslate(CATransform3DIdentity, -500, 10, 0) | |
self.layer.transform = rotationTransform | |
UIView.animate(withDuration: duration) { | |
self.layer.transform = CATransform3DIdentity | |
} | |
} | |
// Fades in cells as user scrolls | |
func fadeInCell(duration: Double) { | |
let rotationTransform = CATransform3DTranslate(CATransform3DIdentity, 0, -10, 0) | |
self.layer.transform = rotationTransform | |
self.alpha = 0 | |
UIView.animate(withDuration: 0.75) { | |
self.alpha = 1.0 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment