iOS Swift 3: 使用 xib 自定义 UIButton
iOS Swift 3: Custom UIButton using xib
我像这样创建了一个自定义 UIButton:
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
}
}
这是我的 HomeButton xib 文件,我也将它命名为 HomeButton.xib
这是 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)
}
}
在 SpringButton 中,我重写了函数:touchesBegan、touchesEnded、touchesMoved、touchesCancelled 以在单击时为按钮设置动画,但这些函数从未被调用。为什么?我这样做是否正确,还是应该采取任何其他方式?
要使用 HomeButton,我只需在故事板中创建一个 IBOutlet 按钮:
我需要以这种方式创建自定义按钮,因为稍后我会在左侧添加一个图像,在右侧添加另一个图标,因此很容易按照我想要的方式自定义按钮。
目前您的自定义按钮的 class 是 HomeButton
,因此它不会响应您在 SpringButton
中覆盖的功能。
正如@karthikeyan 所说,您需要在检查器中将自定义按钮的 class 更改为 SpringButton
。
我像这样创建了一个自定义 UIButton:
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
}
}
这是我的 HomeButton xib 文件,我也将它命名为 HomeButton.xib
这是 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)
}
}
在 SpringButton 中,我重写了函数:touchesBegan、touchesEnded、touchesMoved、touchesCancelled 以在单击时为按钮设置动画,但这些函数从未被调用。为什么?我这样做是否正确,还是应该采取任何其他方式?
要使用 HomeButton,我只需在故事板中创建一个 IBOutlet 按钮:
我需要以这种方式创建自定义按钮,因为稍后我会在左侧添加一个图像,在右侧添加另一个图标,因此很容易按照我想要的方式自定义按钮。
目前您的自定义按钮的 class 是 HomeButton
,因此它不会响应您在 SpringButton
中覆盖的功能。
正如@karthikeyan 所说,您需要在检查器中将自定义按钮的 class 更改为 SpringButton
。