UIButton scale on touch?

Viewed 11502

Can someone tell me how I can scale an UIButton on touch? The button should scale up like 10%.

Thanks in advance!

7 Answers

Swift 5

To make it feel more like native UIButton behaviour I prefer to use touchesBegun and touchesEnded methods in a subclass:

class BaseButton: UIButton {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        UIView.animate(withDuration: 0.3) {
            self.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
            self.titleLabel?.alpha = 0.7
        }
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        UIView.animate(withDuration: 0.3) {
            self.transform = .identity
            self.titleLabel?.alpha = 1
        }
    }
}

Usage

In your storyboard, just make your buttons inherit from BaseButton.

Swift:

button.transform = CGAffineTransform.init(scaleX: 1.0, y: 1.0)

Swift 5:

@objc func onProfileClick(_ sender: CTCircularButton) {
        UIView.animate(withDuration: 0.5, animations: {
            sender.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
        }) { (isComplete) in
            UIView.animate(withDuration: 0.5) {
                sender.transform = .identity
            }
            print("Your action here")
        }
    }
Related