iOS - Swift为UIButton扩展Block点击事件

使用Swift为UIButton扩展Block点击事件,更快捷的完成UIButton事件调用及链式事件的调用

扩展block事件

//
//  UIButton+Action.swift
//  Comic
//
//  Created by 李荣生 on 2022/5/26.
//

import UIKit
import Foundation

typealias ButtonAction = (_ sender:UIButton)->()

extension UIButton{
    
    ///给 button 添加一个属性 用于记录点击的 tag
    private struct AssociatedKeys{
        static var actionKey = "UIButton+LRS+ActionKey"
    }
    @objc dynamic var actionDic: NSMutableDictionary? {
        set{
            objc_setAssociatedObject(self,&AssociatedKeys.actionKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
        }
        get{
            if let dic = objc_getAssociatedObject(self, &AssociatedKeys.actionKey) as? NSDictionary{
                return NSMutableDictionary.init(dictionary: dic)
            }
            return nil
        }
    }
    ///添加对应状态下的点击事件
    @objc dynamic func addTouchAction(action:@escaping  ButtonAction ,for controlEvents: UIControl.Event) {
        
        let eventStr = NSString.init(string: String.init(describing: controlEvents.rawValue))
        if let actions = self.actionDic {
            actions.setObject(action, forKey: eventStr)
            self.actionDic = actions
        }else{
            self.actionDic = NSMutableDictionary.init(object: action, forKey: eventStr)
        }
        
        switch controlEvents {
        case .touchUpInside:
            
            self.addTarget(self, action: #selector(touchUpInSideAction), for: .touchUpInside)
            
        case .touchUpOutside:
            
            self.addTarget(self, action: #selector(touchUpOutsideAction), for: .touchUpOutside)
        
        case .valueChanged:
            
            self.addTarget(self, action: #selector(valueChangedAction), for: .valueChanged)
            ///可以继续添加 其他状态下的点击事件……
        default:
            
            self.addTarget(self, action: #selector(touchUpInSideAction), for: .touchUpInside)
        }
    }
    
    
    @objc fileprivate func touchUpInSideAction(btn: UIButton) {
        if let actionDic = self.actionDic  {
            if let touchUpInSideAction = actionDic.object(forKey: String.init(describing: UIControl.Event.touchUpInside.rawValue)) as? ButtonAction{
                touchUpInSideAction(self)
            }
        }
    }
    
    @objc fileprivate func touchUpOutsideAction(btn: UIButton) {
        if let actionDic = self.actionDic  {
            if let touchUpOutsideButtonAction = actionDic.object(forKey:   String.init(describing: UIControl.Event.touchUpOutside.rawValue)) as? ButtonAction{
                touchUpOutsideButtonAction(self)
            }
        }
    }
    
    @objc fileprivate func valueChangedAction(btn: UIButton) {
        if let actionDic = self.actionDic  {
            if let touchUpOutsideButtonAction = actionDic.object(forKey:   String.init(describing: UIControl.Event.valueChanged.rawValue)) as? ButtonAction{
                touchUpOutsideButtonAction(self)
            }
        }
    }
    
    ///快捷调用 事件类型为 .touchUpInside
    @objc func add_Action(_ action:@escaping ButtonAction){
        self.addTouchAction(action: action, for: .touchUpInside)
    }
    
    
    ///这俩方法就比较的牛了 让每一个点击事件都返回自身,为链式调用做准备
    @discardableResult
    func addTouchUpInSideButtonAction(_ action:@escaping ButtonAction) -> UIButton{
        self.addTouchAction(action: action, for: .touchUpInside)
        return self
    }
    
    @discardableResult
    func addTouchUpOutSideButtonAction(_ action:@escaping ButtonAction) -> UIButton{
        self.addTouchAction(action: action, for: .touchUpOutside)
        return self
    }
    
}

调用方法

     lazy var skipBtn:UIButton = {
        let btn = UIButton(type: .custom)
        
        btn.frame = CGRect(x: (SCREEN_WIDTH - 80.0), y: kStatusBar_Height + k_float_8, width: 60.0, height: 30.0)
        //btn.backgroundColor = .yellow
        btn.setTitleColor(.white_color, for: .normal)
        btn.setTitle("跳过", for:.normal)
        btn.l_makeCornerRadius(radius: 15)
        btn.l_makeBorder(bColor: .white_color, bWidth: 1.0)
        
        ///快捷调用
        btn.add_Action {sender in
           
        }
        
        ///想咋用咋用
        btn.addTouchAction(action:{sender in
            
        }, for: .touchUpInside)
        
        
        ///链式调用
        btn.addTouchUpInSideButtonAction { sender in
            
        }.addTouchUpOutSideButtonAction { sender in
            
        }
        
        return btn
    }()

\color{gray}{欢迎大佬儿来指正纠错,共同学习!!}

你可能感兴趣的:(iOS - Swift为UIButton扩展Block点击事件)