美文网首页
LineView Animation

LineView Animation

作者: lionnner | 来源:发表于2022-11-15 22:19 被阅读0次
    
    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()
        }
    
    }
    
    
    
    

    相关文章

      网友评论

          本文标题:LineView Animation

          本文链接:https://www.haomeiwen.com/subject/ljxsxdtx.html