OOTimerButton.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // OOTimerButton.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/8/29.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. /// 定时器按钮,按下后显示相应的读秒
  10. public class OOTimerButton: UIButton {
  11. //定时时长,单位秒
  12. private var timeLength:Int = 60 {
  13. didSet {
  14. count = timeLength
  15. }
  16. }
  17. private var count:Int = 60
  18. //定时时显示标签
  19. private var timeredLabel:UILabel!
  20. //标签文本颜色
  21. private var labelTextColor:UIColor!
  22. //按钮文本颜色
  23. private var buttonTextColor:UIColor!
  24. //按钮标题
  25. private var buttonTtitle:String!
  26. private var buttonAttributeString:NSAttributedString!
  27. private var disableButtonAttributeString:NSAttributedString!
  28. //定时器
  29. private var timer:Timer!
  30. //定时器状态
  31. private var timerStatus = false
  32. required public init?(coder aDecoder: NSCoder) {
  33. super.init(coder: aDecoder)
  34. }
  35. override init(frame: CGRect) {
  36. super.init(frame: frame)
  37. }
  38. public init(_ timeLength:Int,_ title:String,_ buttonTextColor:UIColor,_ labelTextColor:UIColor) {
  39. super.init(frame: .zero)
  40. self.buttonTtitle = title
  41. self.buttonTextColor = buttonTextColor
  42. self.labelTextColor = labelTextColor
  43. self.layer.cornerRadius = 4
  44. self.layer.masksToBounds = true
  45. self.buttonAttributeString = NSAttributedString(string: buttonTtitle, attributes: [NSAttributedString.Key.foregroundColor:buttonTextColor,NSAttributedString.Key.font:UIFont(name: "PingFangSC-Regular", size: 13.0)!])
  46. self.disableButtonAttributeString = NSAttributedString(string: buttonTtitle, attributes: [NSAttributedString.Key.foregroundColor:UIColor.lightGray,NSAttributedString.Key.font:UIFont(name: "PingFangSC-Regular", size: 13.0)!])
  47. setupUI()
  48. }
  49. public func theme_setButtonTextColor(buttonTextColor:UIColor) {
  50. self.buttonTextColor = buttonTextColor
  51. self.buttonAttributeString = NSAttributedString(string: buttonTtitle, attributes: [NSAttributedString.Key.foregroundColor:buttonTextColor,NSAttributedString.Key.font:UIFont(name: "PingFangSC-Regular", size: 13.0)!])
  52. self.setAttributedTitle(buttonAttributeString, for: .normal)
  53. }
  54. private func setupUI(){
  55. //设置标签
  56. timeredLabel = UILabel(frame:CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
  57. timeredLabel.textAlignment = .center
  58. timeredLabel.textColor = labelTextColor
  59. timeredLabel.font = UIFont(name: "PingFangSC-Regular", size: 13.0)!
  60. timeredLabel.text = ""
  61. //设置按钮
  62. self.setAttributedTitle(buttonAttributeString, for: .normal)
  63. self.setAttributedTitle(disableButtonAttributeString, for: .disabled)
  64. }
  65. public override func layoutSubviews() {
  66. super.layoutSubviews()
  67. }
  68. public func startTiming() {
  69. self.timerStatus = !self.timerStatus
  70. self.isEnabled = false
  71. self.setAttributedTitle(nil, for: .disabled)
  72. self.setAttributedTitle(nil, for: .normal)
  73. timeredLabel.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
  74. timeredLabel.text = "\(count)s \(L10n.Login.recapture)"
  75. self.addSubview(timeredLabel)
  76. if timer != nil {
  77. timer.invalidate()
  78. timer = nil
  79. }
  80. timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timingCall(_:)), userInfo: nil, repeats: true)
  81. timer.fire()
  82. }
  83. public func stopTiming(){
  84. self.timerStatus = !self.timerStatus
  85. if timer != nil {
  86. timer.invalidate()
  87. }
  88. timeredLabel.removeFromSuperview()
  89. //恢复按钮可点击
  90. self.isEnabled = true
  91. self.setAttributedTitle(disableButtonAttributeString, for: .disabled)
  92. self.setAttributedTitle(buttonAttributeString, for: .normal)
  93. //重新计时
  94. count = timeLength
  95. }
  96. @objc private func timingCall(_ sender:Any){
  97. count-=1
  98. if count<=0 {
  99. self.stopTiming()
  100. }else{
  101. timeredLabel.text = "\(count)s \(L10n.Login.recapture)"
  102. self.layoutIfNeeded()
  103. }
  104. }
  105. }