HTPressableButton
HTPressableButton is a customizable UIButton subclass designed to look like it’s being pressed. It provides an animated, tactile effect by shrinking, darkening, and blurring the button when pressed.
Why Use HTPressableButton?
HTPressableButton offers several benefits over a regular UIButton:
- Enhanced user experience: The button gives users visual feedback that it’s being pressed, providing a more engaging and interactive user experience.
- Customizable appearance: HTPressableButton allows you to easily customize the button’s appearance, including colors, shadows, and animations.
- Easy integration: With just a few lines of code, you can integrate HTPressableButton into your project and start using it right away.
Installation
To use HTPressableButton in your project, follow these steps:
- Download the HTPressableButton framework and add it to your project.
- Import the HTPressableButton module in your UIViewController:
import HTPressableButton
You’re now ready to use HTPressableButton in your view.
Usage
Creating and using an HTPressableButton is simple:
// Create an instance of HTPressableButton
let button = HTPressableButton()
// Customize the button's appearance
button.buttonColor = .blue
button.shadowColor = .darkGray
button.shadowHeight = 10
button.cornerRadius = 5
// Set the button's title and font
button.setTitle("Press Me", for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
// Add button targets and actions
button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
// Add the button to your view
self.view.addSubview(button)
Make sure to replace buttonPressed
with your desired action method.
Customization
HTPressableButton can be easily customized to match your app’s design:
Button Color
You can change the button’s color by setting the buttonColor
property to the desired color:
// Set button color to red
button.buttonColor = .red
Shadow
Configure the shadow of the button by adjusting the following properties:
shadowColor:
Sets the color of the button’s shadow.shadowHeight:
Determines the height of the button’s shadow.
// Add a green shadow with a height of 5
button.shadowColor = .green
button.shadowHeight = 5
Corner Radius
You can adjust the corner radius of the button by modifying the cornerRadius
property:
// Set the button's corner radius to 10
button.cornerRadius = 10
Animating the Button
The HTPressableButton animates its appearance when pressed. The animation duration and scale can be adjusted:
Animation Duration
You can set the duration of the animation by modifying the animationDuration
property:
// Set the animation duration to 2 seconds
button.animationDuration = 2
Animation Scale
Adjust the scale of the button during animation with the animationScale
property:
// Set a scale value of 0.9 during animation
button.animationScale = 0.9
Contribution
This project is open to contribution. Feel free to fork and submit pull requests.
License
HTPressableButton is available under the MIT license. See the LICENSE file for more information.