美文网首页
UIButton: 巧妙封装截获响应

UIButton: 巧妙封装截获响应

作者: 十里桃花终是你 | 来源:发表于2018-05-16 18:00 被阅读5次

前言:

        重新写个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的杂乱代码(反正我看着不怎么舒服)

相关文章

  • UIButton: 巧妙封装截获响应

    前言: 重新写个APP 很多东西都要重新来写(之前懒没有将控件剥离出来,现在也懒得去找以前的项目,所以就一...

  • 封装UIView添加UIButton事件无响应

    参考文章:iOS hitTest 自定义封装了UIView,发现内部的 UIButton不响应 addTarget...

  • 一个好用的UIButton扩展

    改变UIButton的响应区域 UIButton+ExtendTouchRect.h UIButton+Exten...

  • iOS知识整理-Block

    Block是将函数及其执行上下文封装起来的对象 变量截获 局部变量截获 是值截获 〜局部静态变量截获 是指针截获 ...

  • 响应链(II)

    扩大UIButton的响应区   通过重载UIButton的 -(BOOL) pointInside: with...

  • RAC 初探

    RAC是利用函数式+响应式编程思想封装的一套框架.为低耦合高内聚开发提供了方便. 一 基本用法 UIButton ...

  • IOS UIButton只响应了一次点击事件问题

    今天在写UIButton点击响应事件时,遇到点击只响应一次的问题,经过查找发现,UIButton的supperVi...

  • UIButton - 封装

    UIButton 是开发中高频使用的控件,一个好的封装能大大提高代码的整洁度 1. Target封装 .........

  • 事件传递和响应者链

    响应者:在iOS中,响应者为能响应事件的UIResponder子类对象,如UIButton、UIView等。 响应...

  • iOS 简单控件封装

    近来无事就把项目中有关封装玩意儿整理一下。话不多说直接上菜。 UIButton 简单封装 UIButton 属性很...

网友评论

      本文标题:UIButton: 巧妙封装截获响应

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