Animate your iOS buttons on touch

By default buttons in iOS do not animate, although you can set different properties for their different states. This simple piece of code can be attached to any button to give it a slight size animation, giving the user the feedback they need to know your button has been pressed.


@IBAction func buttonTouched(_ sender: UIButton) {
UIButton.animate(withDuration: 0.2,
animations: {
sender.transform = CGAffineTransform(scaleX: 0.975, y: 0.96)
},
completion: { finish in
UIButton.animate(withDuration: 0.2, animations: {
sender.transform = CGAffineTransform.identity
})
})
}

Giving your users feedback on their actions is important in maintaining a smooth interface and keeping your user engaged. If you choose to make the animations prettier than this basic example do please share in the comments for others to see.

Thanks for reading!