前言:
? ? ? ? 重新寫個APP 很多東西都要重新來寫(之前懶沒有將控件剝離出來,現(xiàn)在也懶得去找以前的項目盒刚,所以就一波流----重寫吧??)
? ? ? ? 這里需要實現(xiàn)一個常用的短信驗證碼倒計時功能,這玩意兒很多小伙伴都重寫過吧绿贞,所以今天在這里封裝一個專門用來做倒計時的按鈕因块。(老鳥不喜勿噴)
操作:
這是一些比較簡單但是繁瑣的小東西主要用到下面這個函數(shù),以及GCD
- (void)sendAction:(SEL)action
? ? ? ? ? ? ? ? to:(id)target
? ? ? ? ? forEvent:(UIEvent *)event;
這個函數(shù)鏈接: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被點擊了之后會調(diào)用這個函數(shù)來調(diào)用Button的響應(yīng)方法籍铁。
所以在這里我將驗證碼倒計時的邏輯就添加在這個函數(shù)調(diào)用里贮聂,實現(xiàn)帶倒計時功能的自定義Button 以后有項目了隨便拿來用還不用找代碼靠柑。
核心代碼:
這樣就巧妙的實現(xiàn)了Button封裝。而且還避免了使用UIView來自定義一個Button的雜亂代碼(反正我看著不怎么舒服)