Swift 倒計(jì)時(shí)按鈕封裝

參照網(wǎng)上的一個(gè)倒計(jì)時(shí)按鈕焦除,以學(xué)習(xí)的目的抄了一下蕉饼,根據(jù)自己的理解修改了一些內(nèi)容,使倒計(jì)時(shí)按鈕用起來(lái)更方便了些帅戒。因?yàn)槭菐讉€(gè)月前寫(xiě)的灯帮,原作者和鏈接也找不著了,記得是在CocoaChina上找到的逻住。這篇文章只為分享和學(xué)習(xí)施流。

1.先說(shuō)思路

思路很簡(jiǎn)單,創(chuàng)建一個(gè)UIButton鄙信,重寫(xiě)構(gòu)造器方法瞪醋,在實(shí)例上添加一個(gè)UIlabel,然后利用計(jì)時(shí)器在label上顯示倒計(jì)時(shí)數(shù)字装诡。這樣做的原因是如果直接設(shè)置button的title數(shù)字切換時(shí)文字會(huì)閃一下才更新银受。然后可以根據(jù)需要給label添加動(dòng)畫(huà)践盼。本篇大部分代碼都是抄的,里面的旋轉(zhuǎn)動(dòng)畫(huà)和放大動(dòng)畫(huà)占了很大的代碼篇幅宾巍,雖然我覺(jué)得兩個(gè)動(dòng)畫(huà)都不太實(shí)用咕幻,也從來(lái)沒(méi)用過(guò)。

2.不多說(shuō)顶霞,貼代碼肄程,邏輯還是很簡(jiǎn)單的

import UIKit

/**
 動(dòng)畫(huà)類(lèi)型
 */
enum CountDownAnimationType {
    case None       //沒(méi)有動(dòng)畫(huà)
    case Scale      //縮放動(dòng)畫(huà)
    case Rotate     //旋轉(zhuǎn)動(dòng)畫(huà)
}

/// 自定義倒計(jì)時(shí)按鈕
@IBDesignable class CountDownBtn: UIButton {
    /// 倒計(jì)時(shí)中的背景顏色
    @IBInspectable var enabled_bgColor: UIColor = UIColor.clearColor()
    /// 倒計(jì)時(shí)時(shí)數(shù)字顏色
    @IBInspectable var numberColor :UIColor = UIColor.blackColor(){
        didSet{
            timeLabel.textColor = numberColor
        }
    }
    /// 時(shí)間長(zhǎng)度(秒)
    @IBInspectable var count :Int = 0 {
        didSet{
            startCount = count
            originNum = count
        }
    }
    /// 動(dòng)畫(huà)類(lèi)型,默認(rèn)沒(méi)有動(dòng)畫(huà)
    var animaType: CountDownAnimationType = CountDownAnimationType.None
    
    override var frame: CGRect {
        set{
            super.frame = newValue
            timeLabel.frame = frame
        }
        get{
            return super.frame
        }
    }
    override var backgroundColor: UIColor?{
        set{
            super.backgroundColor = newValue
            if normalBgColor == nil {
                normalBgColor = backgroundColor
            }
        }
        get{
            return super.backgroundColor
        }
    }
    private var btnTitle :String?
    private var normalBgColor: UIColor?
    private var timer: NSTimer!
    private var startCount = 0
    private var originNum = 0
    //倒計(jì)時(shí)Label
    private var timeLabel = UILabel()
    override init(frame: CGRect) {
        super.init(frame: frame)
        btnTitle = self.titleForState(.Normal)
        self.addLabel()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        btnTitle = self.titleForState(.Normal)
        self.addLabel()
    }
    private func addLabel() {
        timeLabel.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))
        timeLabel.backgroundColor = UIColor.clearColor()
        timeLabel.font = UIFont.systemFontOfSize(17)//在開(kāi)啟定時(shí)器時(shí)不排除存儲(chǔ)在設(shè)置字號(hào)失敗的情況
        timeLabel.textAlignment = NSTextAlignment.Center
        timeLabel.textColor = numberColor
        timeLabel.text = ""
        self.addSubview(timeLabel)
    }
    
    /**
     開(kāi)啟倒計(jì)時(shí)
     */
    func startCountDown() {
        //設(shè)置為按鈕字號(hào)在addLabel()會(huì)失敗
        timeLabel.font = self.titleLabel?.font
        timeLabel.text = "\(self.originNum)秒"
        self.setTitle("", forState: .Normal)
        self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(CountDownBtn.countDown), userInfo: nil, repeats: true)
        self.backgroundColor = enabled_bgColor
        self.enabled = false
        switch self.animaType {
        case .Scale:
            self.numAnimation()
        case .Rotate:
            self.rotateAnimation()
        default:
            return
        }
    }
    //    倒計(jì)時(shí)開(kāi)始
    @objc private func countDown() {
        self.startCount -= 1
        timeLabel.text = "\(self.startCount)秒"
        
        //倒計(jì)時(shí)完成后停止定時(shí)器,移除動(dòng)畫(huà)
        if self.startCount <= 0 {
            if self.timer == nil {
                return
            }
            self.setTitle(btnTitle, forState: .Normal)
            timeLabel.layer.removeAllAnimations()
            timeLabel.text = ""
            self.timer.invalidate()
            self.timer = nil
            self.enabled = true
            self.startCount = self.originNum
            self.backgroundColor = normalBgColor
        }
    }
    
    //放大動(dòng)畫(huà)
    private func numAnimation() {
        let duration: CFTimeInterval = 1
        let beginTime = CACurrentMediaTime()
        
        // Scale animation
        let scaleAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
        
        scaleAnimation.keyTimes = [0, 0.5, 1]
        scaleAnimation.values = [1, 1.5, 2]
        scaleAnimation.duration = duration
        
        // Opacity animation
        let opacityAnimaton = CAKeyframeAnimation(keyPath: "opacity")
        
        opacityAnimaton.keyTimes = [0, 0.5, 1]
        opacityAnimaton.values = [1, 0.5, 0]
        opacityAnimaton.duration = duration
        
        // Animation
        let animation = CAAnimationGroup()
        
        animation.animations = [scaleAnimation, opacityAnimaton]
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        animation.duration = duration
        animation.repeatCount = HUGE
        animation.removedOnCompletion = false
        
        animation.beginTime = beginTime
        timeLabel.layer.addAnimation(animation, forKey: "animation")
        self.layer.addSublayer(timeLabel.layer)
        
    }
    
    //    旋轉(zhuǎn)變小動(dòng)畫(huà)
    private func rotateAnimation() {
        
        let duration: CFTimeInterval = 1
        let beginTime = CACurrentMediaTime()
        
        // Rotate animation
        let rotateAnimation  = CABasicAnimation(keyPath: "transform.rotation.z")
        rotateAnimation.fromValue = NSNumber(int: 0)
        rotateAnimation.toValue = NSNumber(double: M_PI * 2)
        rotateAnimation.duration = duration;
        
        // Scale animation
        let scaleAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
        scaleAnimation.keyTimes = [0]
        scaleAnimation.values = [1, 2]
        scaleAnimation.duration = 0
        
        // Opacity animation
        let opacityAnimaton = CAKeyframeAnimation(keyPath: "opacity")
        opacityAnimaton.keyTimes = [0, 0.5]
        opacityAnimaton.values = [1, 0]
        opacityAnimaton.duration = duration
        
        // Scale animation
        let scaleAnimation2 = CAKeyframeAnimation(keyPath: "transform.scale")
        scaleAnimation2.keyTimes = [0, 0.5]
        scaleAnimation2.values = [2, 0]
        scaleAnimation2.duration = duration
        
        let animation = CAAnimationGroup()
        
        animation.animations = [rotateAnimation, scaleAnimation, opacityAnimaton, scaleAnimation2]
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        animation.duration = duration
        animation.repeatCount = HUGE
        animation.removedOnCompletion = false
        animation.beginTime = beginTime
        timeLabel.layer.addAnimation(animation, forKey: "animation")
        self.layer.addSublayer(timeLabel.layer)
    }
}

這里簡(jiǎn)單說(shuō)下@IBDesignable和@IBInspectable选浑,@IBDesignable標(biāo)記類(lèi)蓝厌,@IBInspectable標(biāo)記屬性,可以使這個(gè)類(lèi)的屬性在xib和storyboard設(shè)置面板中進(jìn)行設(shè)置古徒,配合didSet可以直接在xib和storyboard面板中顯示實(shí)時(shí)效果拓提。

3.使用

這里只展示下在storyBoard里的用法

a.把代碼放入工程中。

b.拖一個(gè)UIButton隧膘,修改父類(lèi)為CountDownBtn代态。

c.設(shè)置相關(guān)屬性
按鈕的默認(rèn)標(biāo)題,字號(hào),普通狀態(tài)下按鈕文字顏色是在Button里設(shè)置的,頂部三個(gè)屬性分別為倒計(jì)時(shí)狀態(tài)下按鈕背景色疹吃,倒計(jì)時(shí)狀態(tài)下文字顏色蹦疑,倒計(jì)時(shí)的時(shí)間設(shè)置。


storyboard設(shè)置示例

d.點(diǎn)擊事件

 @IBAction func getCodeAction(sender: CountDownBtn) {
        // 設(shè)置動(dòng)畫(huà)類(lèi)型,可以不寫(xiě),默認(rèn)沒(méi)有動(dòng)畫(huà)效果
        sender.animaType = .Rotate
        // 開(kāi)啟倒計(jì)時(shí)
        sender.startCountDown()
        // 這里寫(xiě)其它操作萨驶,如獲取驗(yàn)證碼
    }

這里注意sender的類(lèi)型歉摧,或者自己進(jìn)行轉(zhuǎn)換。

最后

整個(gè)代碼邏輯很簡(jiǎn)單篡撵,可以根據(jù)需要定制下代碼判莉。另外里面的邏輯也可以通過(guò)OC實(shí)現(xiàn)豆挽,didSet方法應(yīng)該可以用KVO代替,或者可以使用set方法(我也不確定)育谬。另外,@IBDesignable和@IBInspectable在OC上的替代方式如下:

IB_DESIGNABLE
@interface CustomButton : UIButton
@property (nonatomic, strong) IBInspectable UIImage *highlightSelectedImage;
@end

如果有誰(shuí)用OC寫(xiě)了請(qǐng)分享我一份??。

有什么地方做的不好帮哈,歡迎指出膛檀。相互學(xué)習(xí)才能更快進(jìn)步。

Demo

Swift4.0更新娘侍,沒(méi)上傳咖刃,自用

import UIKit

/**
 動(dòng)畫(huà)類(lèi)型
 */
enum CountDownAnimationType {
    case None       //沒(méi)有動(dòng)畫(huà)
    case Scale      //縮放動(dòng)畫(huà)
    case Rotate     //旋轉(zhuǎn)動(dòng)畫(huà)
}

/// 自定義倒計(jì)時(shí)按鈕
@IBDesignable class CountDownBtn: UIButton {
    /// 倒計(jì)時(shí)中的背景顏色
    @IBInspectable var enabled_bgColor: UIColor = UIColor.clear
    /// 倒計(jì)時(shí)時(shí)數(shù)字顏色
    @IBInspectable var numberColor :UIColor = UIColor.black{
        didSet{
            timeLabel.textColor = numberColor
        }
    }
    /// 時(shí)間長(zhǎng)度(秒)
    @IBInspectable var count :Int = 0 {
        didSet{
            startCount = count
            originNum = count
        }
    }
    /// 動(dòng)畫(huà)類(lèi)型,默認(rèn)沒(méi)有動(dòng)畫(huà)
    var animaType: CountDownAnimationType = CountDownAnimationType.None
    
    override var frame: CGRect {
        set{
            super.frame = newValue
            timeLabel.frame = frame
        }
        get{
            return super.frame
        }
    }
    override var backgroundColor: UIColor?{
        set{
            super.backgroundColor = newValue
            if normalBgColor == nil {
                normalBgColor = backgroundColor
            }
        }
        get{
            return super.backgroundColor
        }
    }
    private var btnTitle :String?
    private var normalBgColor: UIColor?
    private var timer: Timer!
    private var startCount = 0
    private var originNum = 0
    //倒計(jì)時(shí)Label
    private var timeLabel = UILabel()
    override init(frame: CGRect) {
        super.init(frame: frame)
        btnTitle = self.title(for: .normal)
        self.addLabel()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        btnTitle = self.title(for: .normal)
        self.addLabel()
    }
    private func addLabel() {
        timeLabel.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
            //CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))
        timeLabel.backgroundColor = UIColor.clear
        timeLabel.font = UIFont.systemFont(ofSize: 17)//在開(kāi)啟定時(shí)器時(shí)不排除存儲(chǔ)在設(shè)置字號(hào)失敗的情況
        timeLabel.textAlignment = NSTextAlignment.center
        timeLabel.textColor = numberColor
        timeLabel.text = ""
        self.addSubview(timeLabel)
    }
    
    /**
     開(kāi)啟倒計(jì)時(shí)
     */
    func startCountDown() {
        //設(shè)置為按鈕字號(hào)在addLabel()會(huì)失敗
        timeLabel.font = self.titleLabel?.font
        timeLabel.text = "\(self.originNum)秒"
        self.setTitle("", for: .normal)
        self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(CountDownBtn.countDown), userInfo: nil, repeats: true)
        self.backgroundColor = enabled_bgColor
        self.isEnabled = false
        switch self.animaType {
        case .Scale:
            self.numAnimation()
        case .Rotate:
            self.rotateAnimation()
        default:
            return
        }
    }
    //    倒計(jì)時(shí)開(kāi)始
    @objc private func countDown() {
        self.startCount -= 1
        timeLabel.text = "\(self.startCount)秒"
        
        //倒計(jì)時(shí)完成后停止定時(shí)器,移除動(dòng)畫(huà)
        if self.startCount <= 0 {
            if self.timer == nil {
                return
            }
            self.setTitle(btnTitle, for: .normal)
            timeLabel.layer.removeAllAnimations()
            timeLabel.text = ""
            self.timer.invalidate()
            self.timer = nil
            self.isEnabled = true
            self.startCount = self.originNum
            self.backgroundColor = normalBgColor
        }
    }
    
    //放大動(dòng)畫(huà)
    private func numAnimation() {
        let duration: CFTimeInterval = 1
        let beginTime = CACurrentMediaTime()
        
        // Scale animation
        let scaleAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
        
        scaleAnimation.keyTimes = [0, 0.5, 1]
        scaleAnimation.values = [1, 1.5, 2]
        scaleAnimation.duration = duration
        
        // Opacity animation
        let opacityAnimaton = CAKeyframeAnimation(keyPath: "opacity")
        
        opacityAnimaton.keyTimes = [0, 0.5, 1]
        opacityAnimaton.values = [1, 0.5, 0]
        opacityAnimaton.duration = duration
        
        // Animation
        let animation = CAAnimationGroup()
        
        animation.animations = [scaleAnimation, opacityAnimaton]
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        animation.duration = duration
        animation.repeatCount = HUGE
        animation.isRemovedOnCompletion = false
        
        animation.beginTime = beginTime
        timeLabel.layer.add(animation, forKey: "animation")
        self.layer.addSublayer(timeLabel.layer)
        
    }
    
    //    旋轉(zhuǎn)變小動(dòng)畫(huà)
    private func rotateAnimation() {
        
        let duration: CFTimeInterval = 1
        let beginTime = CACurrentMediaTime()
        
        // Rotate animation
        let rotateAnimation  = CABasicAnimation(keyPath: "transform.rotation.z")
        rotateAnimation.fromValue = NSNumber(value: 0)
        rotateAnimation.toValue = NSNumber(value: Double.pi * 2)
        rotateAnimation.duration = duration;
        
        // Scale animation
        let scaleAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
        scaleAnimation.keyTimes = [0]
        scaleAnimation.values = [1, 2]
        scaleAnimation.duration = 0
        
        // Opacity animation
        let opacityAnimaton = CAKeyframeAnimation(keyPath: "opacity")
        opacityAnimaton.keyTimes = [0, 0.5]
        opacityAnimaton.values = [1, 0]
        opacityAnimaton.duration = duration
        
        // Scale animation
        let scaleAnimation2 = CAKeyframeAnimation(keyPath: "transform.scale")
        scaleAnimation2.keyTimes = [0, 0.5]
        scaleAnimation2.values = [2, 0]
        scaleAnimation2.duration = duration
        
        let animation = CAAnimationGroup()
        
        animation.animations = [rotateAnimation, scaleAnimation, opacityAnimaton, scaleAnimation2]
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        animation.duration = duration
        animation.repeatCount = HUGE
        animation.isRemovedOnCompletion = false
        animation.beginTime = beginTime
        timeLabel.layer.add(animation, forKey: "animation")
        self.layer.addSublayer(timeLabel.layer)
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末憾筏,一起剝皮案震驚了整個(gè)濱河市嚎杨,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌氧腰,老刑警劉巖枫浙,帶你破解...
    沈念sama閱讀 222,183評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件刨肃,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡箩帚,警方通過(guò)查閱死者的電腦和手機(jī)真友,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)紧帕,“玉大人盔然,你說(shuō)我怎么就攤上這事∈鞘龋” “怎么了愈案?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,766評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)叠纷。 經(jīng)常有香客問(wèn)我刻帚,道長(zhǎng),這世上最難降的妖魔是什么涩嚣? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,854評(píng)論 1 299
  • 正文 為了忘掉前任崇众,我火速辦了婚禮,結(jié)果婚禮上航厚,老公的妹妹穿的比我還像新娘顷歌。我一直安慰自己,他們只是感情好幔睬,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布眯漩。 她就那樣靜靜地躺著,像睡著了一般麻顶。 火紅的嫁衣襯著肌膚如雪赦抖。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,457評(píng)論 1 311
  • 那天辅肾,我揣著相機(jī)與錄音队萤,去河邊找鬼。 笑死矫钓,一個(gè)胖子當(dāng)著我的面吹牛要尔,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播新娜,決...
    沈念sama閱讀 40,999評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼赵辕,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了概龄?” 一聲冷哼從身側(cè)響起还惠,我...
    開(kāi)封第一講書(shū)人閱讀 39,914評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎私杜,沒(méi)想到半個(gè)月后蚕键,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體互拾,經(jīng)...
    沈念sama閱讀 46,465評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評(píng)論 3 342
  • 正文 我和宋清朗相戀三年嚎幸,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了颜矿。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,675評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡嫉晶,死狀恐怖骑疆,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情替废,我是刑警寧澤箍铭,帶...
    沈念sama閱讀 36,354評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站椎镣,受9級(jí)特大地震影響诈火,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜状答,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評(píng)論 3 335
  • 文/蒙蒙 一冷守、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧惊科,春花似錦拍摇、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,514評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至蜡娶,卻和暖如春混卵,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背窖张。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,616評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工幕随, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人荤堪。 一個(gè)月前我還...
    沈念sama閱讀 49,091評(píng)論 3 378
  • 正文 我出身青樓合陵,卻偏偏與公主長(zhǎng)得像枢赔,于是被迫代替她去往敵國(guó)和親澄阳。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評(píng)論 2 360

推薦閱讀更多精彩內(nèi)容