UIButton: 巧妙封装截获响应

前言:

        重新写个APP 很多东西都要重新来写(之前懒没有将控件剥离出来,现在也懒得去找以前的项目,所以就一波流----重写吧☹️)

        这里需要实现一个常用的短信验证码倒计时功能,这玩意儿很多小伙伴都重写过吧,所以今天在这里封装一个专门用来做倒计时的按钮。(老鸟不喜勿喷)

操作:

这是一些比较简单但是繁琐的小东西主要用到下面这个函数,以及GCD

- (void)sendAction:(SEL)action

                to:(id)target

          forEvent:(UIEvent *)event;

这个函数链接:sendAction:to:forEvent:

文档描述为:

This method takes the provided information and forwards it to the singleton UIApplicationobject for dispatching. If a valid target object was supplied, the app calls the action method on that target object. If the target object is nil, the app searches the responder chain for an object that defines the method.

Subclasses may override this method and use it to observe or modify the action dispatching behavior. Implementations should call super when they want to continue with the execution of the action method.

这里的用处就是当Button被点击了之后会调用这个函数来调用Button的响应方法。

所以在这里我将验证码倒计时的逻辑就添加在这个函数调用里,实现带倒计时功能的自定义Button 以后有项目了随便拿来用还不用找代码。

核心代码:


核心代码

这样就巧妙的实现了Button封装。而且还避免了使用UIView来自定义一个Button的杂乱代码(反正我看着不怎么舒服)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 12,273评论 3 20
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,448评论 0 10
  • 一天的工作下来,花店老板natasha 从地球的另一端带回了埃及的香精,这是我有意识以来闻到的最美好的香味...
    T伊恩阅读 222评论 0 0
  • 今天,坚持我的运动,把脚步迈的稍微快一点,试试自己是不是能承受的住,这些天由于自己的身体状况不是特别好,一再出现症...
    边云兴阅读 134评论 0 1
  • 过自己想要的生活,上帝会让你付出代价。照顾好自己,爱自己才能爱好别人。如果你压抑,痛苦,忧伤,不自由,又怎么可能在...
    浪漫的高贵阅读 908评论 2 5