Reputation: 39
I Have Already made the Outlets and the action. I have linked the code to the UI.
I want a button to be highlighted when pressed. This the code I wrote:
@IBAction func buttonPressed(_ sender: UIButton) {
sender.setTitleColor(UIColor(GL_GREEN), for: .highlighted)
}
Upvotes: 3
Views: 16420
Reputation: 11
Easy way: Built-in Highlight on Tap.
Built-in property showsTouchWhenHighlighted, which causes a brief highlight (a "blink") on tap. This works especially well if you’re looking for a quick solution.
// Enable highlight effect on button tap
button.showsTouchWhenHighlighted = true
Custom animation
If you need more control over the button’s appearance on tap, you can use custom code to handle different tap events:
Step 1: Set up button event listeners for different touch states.
func setupButton() {
// Set up button properties
// ...
// Set up events to handle button highlight and release effects
// Button touch started
button.addTarget(self, action: #selector(buttonTouchDown), for: .touchDown)
// Touch released within button boundaries > functional under button should happen
button.addTarget(self, action: #selector(buttonTouchUpInside), for: .touchUpInside)
// Touch released out of button boundaries > functional under button won't happen
button.addTarget(self, action: #selector(buttonTouchUpOutside), for: .touchUpOutside)
}
Step 2: Implement button state handlers
// Set background color when button is pressed
@objc private func buttonTouchDown() {
ctaButton.backgroundColor = .orange
}
// Set background color with animation when tap released within button boundaries
// and animate it's changing back
@objc private func buttonTouchUpInside() {
button.backgroundColor = UIColor.orange
UIView.animate(withDuration: 0.2, animations: { [weak self] in
self?.button.backgroundColor = .clear
}, completion: { [weak self] _ in
// Do action within this completion, if UI going to change after the tap👆
})
// OR do action here, if UI won't change dramatically🙂
}
// Reset background color when button is released outside
@objc private func buttonTouchUpOutside() {
button.backgroundColor = .clear
}
Upvotes: 1
Reputation: 403
In 2021...
You just need to set the ButtonType to be .system and it will highlight when pressed.
let button = UIButton(type: .system)
Here's a button I made, for clarity:
lazy var myButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Show More", for: .normal)
button.setTitleColor(.systemBlue, for: .normal)
button.titleLabel?.font = .systemFont(ofSize: 17, weight: .regular)
button.layer.masksToBounds = true
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
return button
}()
@objc fileprivate func buttonTapped() {
//code for segue
}
Upvotes: 2
Reputation: 202
You can subclass a UIButton. Create a new swift file and name it anything you want but in this example I will name it HighlightedButton. Add below code in the new file you created. The class is named same as the file you created.
import UIKit
class HighlightedButton: UIButton {
override var isHighlighted: Bool {
didSet {
backgroundColor = isHighlighted ? .red : .green
}
}
}
Next step is to set HighlightedButton as the class of your button:
In storyboard choose the UIButton you want to change. In the identity inspector in the right corner there is a filed named "class" there type "HighlightedButton" and press enter. Now your button will change color to red when it is highlighted and back to green when you release the button.
You can change to any color you want.
Upvotes: 12
Reputation: 81
You have to make 2 actions one is "Touch down" and "Touch up inside" action . change background color for both of the action and you will achieve your target
Upvotes: 5