Skip to content

Commit 5601874

Browse files
committed
修复计时器存在的显示问题
1 parent cd3cc32 commit 5601874

File tree

2 files changed

+10
-38
lines changed

2 files changed

+10
-38
lines changed

VPet-Mac/AnimePlayer.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class AnimePlayer{
8585

8686
// 计时器数字Label
8787
var timerLabel: NSTextField!
88-
var workStartTime: Date?
88+
var workTimerPassedSeconds:Int = 0;
8989
var workTimer: Timer?
9090

9191

@@ -451,28 +451,30 @@ class AnimePlayer{
451451

452452
// 启动计时器
453453
func startWorkTimer() {
454-
workStartTime = Date()
455454
timerLabel.isHidden = false
456455
updateWorkTimerLabel()
457456
workTimer?.invalidate()
458457
workTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
459458
self?.updateWorkTimerLabel()
460459
}
460+
// 防止用户在按下按钮不松开的时候计时器卡住,需要把计时器设置为.common模式
461+
RunLoop.current.add(workTimer!, forMode: .common)
461462
}
462463
// 停止计时器
463464
func stopWorkTimer() {
464465
workTimer?.invalidate()
465466
workTimer = nil
466467
timerLabel.isHidden = true
468+
workTimerPassedSeconds = 0
467469
timerLabel.stringValue = "00:00:00"
468470
}
469471
// 更新时间显示
470472
func updateWorkTimerLabel() {
471-
guard let start = workStartTime else { timerLabel.stringValue = "00:00:00"; return }
472-
let interval = Int(Date().timeIntervalSince(start))
473-
let hours = interval / 3600
474-
let minutes = (interval % 3600) / 60
475-
let seconds = interval % 60
473+
workTimerPassedSeconds += 1;
474+
var interval = workTimerPassedSeconds
475+
let hours = interval / 3600; interval %= 3600;
476+
let minutes = interval / 60; interval %= 60;
477+
let seconds = interval;
476478
timerLabel.stringValue = String(format: "%02d:%02d:%02d", hours, minutes, seconds)
477479
}
478480
}

VPet-Mac/ViewController.swift

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ class ViewController: NSViewController {
3737
return label
3838
}()
3939

40-
// 计时器相关变量
41-
var workStartTime: Date?
42-
var workTimer: Timer?
43-
4440
override func viewDidLoad() {
4541
print("viewdidload")
4642
super.viewDidLoad()
@@ -94,6 +90,7 @@ class ViewController: NSViewController {
9490
return event
9591
}
9692
VPET.handleLeftMouseDown(event.locationInWindow)
93+
break;
9794
case .rightMouseUp:
9895
break;
9996
case .leftMouseDragged:
@@ -182,33 +179,6 @@ class ViewController: NSViewController {
182179
}
183180
}
184181

185-
// 启动计时器
186-
func startWorkTimer() {
187-
workStartTime = Date()
188-
timerLabel.isHidden = false
189-
updateWorkTimerLabel()
190-
workTimer?.invalidate()
191-
workTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
192-
self?.updateWorkTimerLabel()
193-
}
194-
}
195-
// 停止计时器
196-
func stopWorkTimer() {
197-
workTimer?.invalidate()
198-
workTimer = nil
199-
timerLabel.isHidden = true
200-
timerLabel.stringValue = "00:00:00"
201-
}
202-
// 更新时间显示
203-
func updateWorkTimerLabel() {
204-
guard let start = workStartTime else { timerLabel.stringValue = "00:00:00"; return }
205-
let interval = Int(Date().timeIntervalSince(start))
206-
let hours = interval / 3600
207-
let minutes = (interval % 3600) / 60
208-
let seconds = interval % 60
209-
timerLabel.stringValue = String(format: "%02d:%02d:%02d", hours, minutes, seconds)
210-
}
211-
212182
override func mouseMoved(with event: NSEvent) {
213183
// 移除鼠标移动事件处理,因为我们不再需要它
214184
}

0 commit comments

Comments
 (0)