I created a custom UIButton like this:
import UIKit
class HomeButton: UIButton {
@IBOutlet weak var viewBG: UIView!
@IBInspectable var fontSize: CGFloat = 22
@IBInspectable var bgColor: UIColor? = UIColor.white{
didSet{
viewBG.backgroundColor = bgColor
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
override func layoutSubviews() {
super.layoutSubviews()
self.titleLabel?.font = UIFont(name: "Lato-Bold", size: fontSize)
}
func setup() {
let view = loadViewFromNib(nibName: "HomeButton") as! SpringButton
view.frame = self.bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(view)
self.clipsToBounds = true
self.titleLabel?.textColor = UIColor.white
self.setTitleColor(UIColor.white, for: UIControlState.normal)
self.layer.cornerRadius = CGFloat(K.CORNER_RADIUS)
self.backgroundColor = UIColor.clear
}
}
and here's my xib file for HomeButton, I also named it HomeButton.xib
Here's SpringButton class
import UIKit
class SpringButton: UIButton {
open var minimumScale: CGFloat = 0.95
open var pressSpringDamping: CGFloat = 0.4
open var releaseSpringDamping: CGFloat = 0.35
open var pressSpringDuration = 0.4
open var releaseSpringDuration = 0.5
public init() {
super.init(frame: CGRect.zero)
}
required public init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
UIView.animate(withDuration: self.pressSpringDuration, delay: 0, usingSpringWithDamping: self.pressSpringDamping, initialSpringVelocity: 0, options: [.curveLinear, .allowUserInteraction], animations: { () -> Void in
self.transform = CGAffineTransform(scaleX: self.minimumScale, y: self.minimumScale)
}, completion: nil)
}
override open func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
UIView.animate(withDuration: self.releaseSpringDuration, delay: 0, usingSpringWithDamping: self.releaseSpringDamping, initialSpringVelocity: 0, options: [.curveLinear, .allowUserInteraction], animations: { () -> Void in
self.transform = CGAffineTransform.identity
}, completion: nil)
}
override open func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let location = touches.first!.location(in: self)
if !self.bounds.contains(location) {
UIView.animate(withDuration: self.releaseSpringDuration, delay: 0, usingSpringWithDamping: self.releaseSpringDamping, initialSpringVelocity: 0, options: [.curveLinear, .allowUserInteraction], animations: { () -> Void in
self.transform = CGAffineTransform.identity
}, completion: nil)
}
}
override open func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)
UIView.animate(withDuration: self.releaseSpringDuration, delay: 0, usingSpringWithDamping: self.releaseSpringDamping, initialSpringVelocity: 0, options: [.curveLinear, .allowUserInteraction], animations: { () -> Void in
self.transform = CGAffineTransform.identity
}, completion: nil)
}
}
In SpringButton, I override functions: touchesBegan, touchesEnded, touchesMoved, touchesCancelled to animate the button when click, but those functions are never called. Why? Am I doing this correctly or should I do any other way?
To use the HomeButton, I just create an IBOutlet button in the storyboard:
I need to create the custom button this way because I'll later add an image on the left and another icon on the right, so it's easy to customize the button the way I want.
For now your customized button's class is HomeButton
so it does not response functions you override in SpringButton
.
As @karthikeyan say you need change your customized button's class to SpringButton
in inspector.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.