Timer+LWPlayer.swift 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. //
  2. // Timer+LWPlayer.swift
  3. // SwiftBilibili
  4. //
  5. // Created by 罗文 on 2021/4/2.
  6. // Copyright © 2021年 罗文. All rights reserved.
  7. //
  8. import Foundation
  9. public extension Timer {
  10. class func scheduledTimerWithTimeInterval(_ timeInterval: TimeInterval, block: ()->(), repeats: Bool) -> Timer {
  11. return self.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(Timer.executeBlockWithTimer(_:)), userInfo: AnyObject.self, repeats: repeats)
  12. }
  13. class func timerWithTimeInterval(_ timeInterval: TimeInterval, block: ()->(), repeats: Bool) -> Timer {
  14. return Timer(timeInterval: timeInterval, target: self, selector: #selector(Timer.executeBlockWithTimer(_:)), userInfo: AnyObject.self, repeats: repeats)
  15. }
  16. @objc private class func executeBlockWithTimer(_ timer: Timer) {
  17. // let block: ()->() = timer.userInfo as! ()->()
  18. // block()
  19. }
  20. static func executeOnMainQueueAfterTimeInterval(_ seconds: TimeInterval,block: @escaping ()->()) {
  21. executeAfterTimeInterval(seconds, queue: DispatchQueue.main, block: block)
  22. }
  23. static func executeAfterTimeInterval(_ seconds: TimeInterval, queue: DispatchQueue, block: @escaping ()->()) {
  24. let time = DispatchTime.now() + Double(Int64(seconds * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
  25. queue.asyncAfter(deadline: time, execute: block)
  26. }
  27. }