import UIKit
class LineView: UIView, XibViewProtocol {
@IBOutlet var contentView: UIView!
@IBOutlet var widthC: NSLayoutConstraint!
private var timer: DispatchSourceTimer?
override init(frame: CGRect) {
super.init(frame: frame)
loadNib()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
loadNib()
}
override func awakeFromNib() {
super.awakeFromNib()
}
private func setTimer(){
timer?.setEventHandler {[weak self] in
guard let `self` = self else {
return
}
self.seconds -= 0.1
print("seconds = == == = \(self.seconds)")
if self.seconds < 0 {
self.timer?.cancel()
self.widthC.constant = 0.0
print("secondsOVER!!!!!!")
} else {
UIView.animate(withDuration: 0.1) {
self.widthC.constant += (self.bounds.size.width / self.total) * 0.1
self.layoutIfNeeded()
}
}
}
}
private var seconds: TimeInterval = 0.0
private var total: TimeInterval = 1.0
@objc public func startAnimate(time: TimeInterval){
timer?.cancel()
timer = nil
self.widthC.constant = 0.0
timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)
total = time
seconds = time
timer?.schedule(deadline: .now(),repeating: 0.1)
setTimer()
timer?.resume()
}
}
网友评论