Animate your iOS buttons on touch

By default, buttons in iOS do not animate. Fortunately, you can set alternative properties for their different states. This simple piece of code can be attached to any button to give it a slight size animation. Therefore the user gets 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 like this 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!