最简单使用
let btn = UIButton.init {
// 按钮点击事件
}
// 可以进行其他按钮设置
使用按钮
override func viewDidLoad() {
super.viewDidLoad()
let btn = UIButton.init(setImage: "首页未选中", frame: CGRect.init(x:100, y: 100, width: 100, height: 100)) {
// 点击的回调
print("btn")
}
view.addSubview(btn)
}
实现原理(可以根据需要自行扩展)
import UIKit
typealias buttonClick = (()->()) // 定义数据类型(其实就是设置别名)
extension UIButton{
// 改进写法【推荐】
private struct RuntimeKey {
static let actionBlock = UnsafeRawPointer.init(bitPattern: "actionBlock".hashValue)
/// ...其他Key声明
}
/// 运行时关联
private var actionBlock: buttonClick? {
set {
objc_setAssociatedObject(self, UIButton.RuntimeKey.actionBlock!, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
}
get {
return objc_getAssociatedObject(self, UIButton.RuntimeKey.actionBlock!) as? buttonClick
}
}
/// 点击回调
@objc func tapped(button:UIButton){
if self.actionBlock != nil {
self.actionBlock!()
}
}
/// 快速创建
convenience init(action:@escaping buttonClick){
self.init()
self.addTarget(self, action:#selector(tapped(button:)) , for:.touchUpInside)
self.actionBlock = action
self.sizeToFit()
}
/// 快速创建
convenience init(setImage:String, action:@escaping buttonClick){
self.init()
self.frame = frame
self.setImage(UIImage(named:setImage), for: UIControlState.normal)
self.addTarget(self, action:#selector(tapped(button:)) , for:.touchUpInside)
self.actionBlock = action
self.sizeToFit()
}
/// 快速创建按钮 setImage: 图片名 frame:frame action:点击事件的回调
convenience init(setImage:String, frame:CGRect, action: @escaping buttonClick){
self.init( setImage: setImage, action: action)
self.frame = frame
}
}