 
 import UIKit
 
-class TLTimeButton: UIButton {
+@objc public protocol TLTimeButtonDelegate {
     
-    var timeLine = 60 //倒计时时长，默认60s
-    var originTitle = "获取验证码"
+    optional func timeWillRun()
+    optional func timeRunning(currentTime: Int)
+    optional func timeDidRun()
+}
+
+public class TLTimeButton: UIButton {
+    
+    var timeLine: Int = 60 //倒计时初始时间
+    var originTitle: String! //原始文案显示
     var finishTitle: String? //倒计时完成文案，如果未指定，则显示originTitle文案
-    var timeRunningUnit = "秒" //倒计时时，秒数后面显示的单位
-    var enableColor = UIColor.redColor()
-    var disableColor = UIColor.grayColor()
+    var timeRunningUnit: String? //倒计时时数字后面的单位，如果为空则不显示
+    var enableColor: UIColor! //使能时的颜色
+    var disableColor: UIColor! //不使能时的颜色
+    
+    weak var delegate: TLTimeButtonDelegate?
     
     override init(frame: CGRect) {
         super.init(frame: frame)
+        
     }
-
-    required init?(coder aDecoder: NSCoder) {
+    
+    required public init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
     
-    func startWithTime() {
+    convenience public init(frame: CGRect, timeLine: Int, originTitle: String, finishTitle: String?, timeRunningUnit: String?, enableColor: UIColor, disableColor: UIColor) {
+        self.init(frame: frame)
+        
+        self.timeLine = timeLine
+        self.originTitle = originTitle
+        self.finishTitle = finishTitle
+        self.timeRunningUnit = timeRunningUnit
+        self.enableColor = enableColor
+        self.disableColor = disableColor
+        
+        styleInit()
+    }
+    
+    private func styleInit() {
+        self.userInteractionEnabled = true
+        self.backgroundColor = enableColor
+        self.setTitle(originTitle, forState: .Normal)
+        self.addTarget(self, action: "clickButton:", forControlEvents: .TouchUpInside)
+    }
+    
+    private func clickButton(sender: TLTimeButton){
+        
+        if let theDelegate = delegate {
+            if let theMethod = theDelegate.timeWillRun?() {
+                theMethod
+            }
+        }
+        
+        startWithTime()
+    }
+    
+    private func startWithTime() {
         
         var timeOut = timeLine
         

                    self.userInteractionEnabled = false
                })
                
                timeOut--
            }
        }
        
        dispatch_resume(timer)
        
    }
    
}

                
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    self.backgroundColor = self.enableColor
                    self.setTitle(self.finishTitle ?? self.originTitle, forState: .Normal)
                    self.userInteractionEnabled = true
                })
                
                if let theDelegate = self.delegate {
                    if let theMethod = theDelegate.timeDidRun?() {
                        theMethod
                    }
                }
                
            }else{
                let timeStr = String(timeOut)+(self.timeRunningUnit ?? "")
                
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    self.backgroundColor = self.disableColor
