美文网首页
一行代码倒计时和防止按钮重复点击原理

一行代码倒计时和防止按钮重复点击原理

作者: 小堆同学 | 来源:发表于2016-12-28 17:19 被阅读90次

<b>一行代码倒计时</b>
实例:

[_btn ss_countDown:3 title:@"s再发送"];

比较简单,<a href="https://github.com/scrumsnail/SSCountDown">demo地址</a>

<b>防止按钮重复点击原理</b>

点击按钮,按钮底层实现代码

(_ action: Selector, to target: AnyObject?, for event: UIEvent?)

利用润runtime的method_exchangeImplementations方法改变自带底层实现的方法

     override open class func initialize() {
        let a :Method = class_getInstanceMethod(self, #selector(UIButton.sendAction(_:to:for:)))
        let b :Method = class_getInstanceMethod(self, #selector(UIButton.ss_sendAction(_:to:for:)))
        method_exchangeImplementations(a, b)
    }

    func ss_sendAction(_ action: Selector, to target: AnyObject?, for event: UIEvent?) {
        if self.ss_ignore_event {
            return
        }

        if self.ss_accepet_time > 0 {
            self.ss_ignore_event = true
            self.perform(#selector(setter: ss_ignore_event), with: false, afterDelay: TimeInterval(self.ss_accepet_time))
        }

        self.ss_sendAction(action, to: target, for: event)
    }

<a href="https://github.com/scrumsnail/ButtonHandle">demo地址</a>

相关文章

网友评论

      本文标题:一行代码倒计时和防止按钮重复点击原理

      本文链接:https://www.haomeiwen.com/subject/ibgyvttx.html