extension UIButton {
func wiggle(){
let wiggleAnimation = CABasicAnimation(keyPath: "position")
wiggleAnimation.duration = 0.05
wiggleAnimation.repeatCount = 5
wiggleAnimation.autoreverses = true
wiggleAnimation.fromValue = CGPoint(x: self.center.x - 4, y: self.center.y)
wiggleAnimation.toValue = CGPoint(x: self.center.x + 4, y: self.center.y)
layer.add(wiggleAnimation, forKey: "position")
}
func fade(){
UIView.animate(withDuration: 0.5, animations: {
self.alpha = 0.75
}) {(finished) in
UIView.animate(withDuration: 0.15, animations: {
self.alpha = 1.0
})
}
}
func colorize(){
let randomNumberArray = generateRandomNumbers(quantity: 3)
let randomColor = UIColor(red: randomNumberArray[0]/255, green: randomNumberArray[1]/255, blue: randomNumberArray[2]/255, alpha: 1)
UIView.animate(withDuration: 0.3) {
self.backgroundColor = randomColor
}
}
}
//Seperate File
import UIKit
func generateRandomNumbers(quantity:Int)->[CGFloat] {
var randomNumberArray = [CGFloat]()
for _ in 1...quantity {
let randomNumber = CGFloat(arc4random_uniform(255))
randomNumberArray.append(randomNumber)
}
return randomNumberArray
}