Skip to content

Commit cd3cc32

Browse files
committed
清理计时器涉及的冗余逻辑
1 parent 64a539a commit cd3cc32

File tree

7 files changed

+15
-193
lines changed

7 files changed

+15
-193
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
.DS_Store
1+
.DS_Store
2+
*.xcuserstate
3+
xcuserdata/

VPet-Mac.xcodeproj/project.pbxproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,12 @@
477477
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
478478
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
479479
CODE_SIGN_ENTITLEMENTS = "VPet-Mac/VPet-Mac.entitlements";
480+
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "-";
480481
CODE_SIGN_STYLE = Automatic;
481482
COMBINE_HIDPI_IMAGES = YES;
482483
CURRENT_PROJECT_VERSION = 1;
483484
DEAD_CODE_STRIPPING = YES;
484-
DEVELOPMENT_TEAM = SMVBX9SZXX;
485+
DEVELOPMENT_TEAM = "";
485486
GENERATE_INFOPLIST_FILE = YES;
486487
INFOPLIST_FILE = "";
487488
INFOPLIST_KEY_LSUIElement = YES;
@@ -507,11 +508,12 @@
507508
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
508509
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
509510
CODE_SIGN_ENTITLEMENTS = "VPet-Mac/VPet-Mac.entitlements";
511+
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "-";
510512
CODE_SIGN_STYLE = Automatic;
511513
COMBINE_HIDPI_IMAGES = YES;
512514
CURRENT_PROJECT_VERSION = 1;
513515
DEAD_CODE_STRIPPING = YES;
514-
DEVELOPMENT_TEAM = SMVBX9SZXX;
516+
DEVELOPMENT_TEAM = "";
515517
GENERATE_INFOPLIST_FILE = YES;
516518
INFOPLIST_FILE = "";
517519
INFOPLIST_KEY_LSUIElement = YES;
-40.6 KB
Binary file not shown.

VPet-Mac.xcodeproj/project.xcworkspace/xcuserdata/justaloli.xcuserdatad/WorkspaceSettings.xcsettings

Lines changed: 0 additions & 14 deletions
This file was deleted.

VPet-Mac.xcodeproj/project.xcworkspace/xcuserdata/justaloli.xcuserdatad/xcdebugger/Expressions.xcexplist

Lines changed: 0 additions & 30 deletions
This file was deleted.

VPet-Mac/AnimePlayer.swift

Lines changed: 7 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -84,80 +84,10 @@ class AnimePlayer{
8484
var studySecondsElapsed: Int = 0
8585

8686
// 计时器数字Label
87-
var timerLabel: NSTextField = {
88-
let label = NSTextField(labelWithString: "00:00:00")
89-
label.font = NSFont.monospacedDigitSystemFont(ofSize: 32, weight: .bold)
90-
label.textColor = .white
91-
label.isBezeled = false
92-
label.drawsBackground = false
93-
label.isEditable = false
94-
label.isSelectable = false
95-
label.alignment = .center
96-
label.isHidden = true
97-
label.backgroundColor = .clear
98-
return label
99-
}()
87+
var timerLabel: NSTextField!
10088
var workStartTime: Date?
10189
var workTimer: Timer?
10290

103-
// 学习计时器面板UI
104-
class StudyPanelView: NSView {
105-
let titleLabel = NSTextField(labelWithString: "当前正在学习")
106-
let timerLabel = NSTextField(labelWithString: "00:00:00")
107-
let stopButton = NSButton(title: "停止学习", target: nil, action: nil)
108-
var stopAction: (() -> Void)?
109-
110-
override init(frame: NSRect) {
111-
super.init(frame: frame)
112-
wantsLayer = true
113-
layer?.backgroundColor = NSColor(white: 0, alpha: 0.7).cgColor
114-
layer?.cornerRadius = 16
115-
// 标题
116-
titleLabel.font = NSFont.boldSystemFont(ofSize: 20)
117-
titleLabel.textColor = .white
118-
titleLabel.alignment = .center
119-
// 计时器
120-
timerLabel.font = NSFont.monospacedDigitSystemFont(ofSize: 36, weight: .bold)
121-
timerLabel.textColor = .white
122-
timerLabel.alignment = .center
123-
// 按钮
124-
stopButton.font = NSFont.boldSystemFont(ofSize: 18)
125-
stopButton.bezelStyle = .rounded
126-
stopButton.contentTintColor = .white
127-
stopButton.wantsLayer = true
128-
stopButton.layer?.backgroundColor = NSColor(white: 1, alpha: 0.15).cgColor
129-
stopButton.layer?.cornerRadius = 8
130-
stopButton.action = #selector(stopButtonPressed)
131-
stopButton.target = self
132-
// 布局
133-
titleLabel.translatesAutoresizingMaskIntoConstraints = false
134-
timerLabel.translatesAutoresizingMaskIntoConstraints = false
135-
stopButton.translatesAutoresizingMaskIntoConstraints = false
136-
addSubview(titleLabel)
137-
addSubview(timerLabel)
138-
addSubview(stopButton)
139-
NSLayoutConstraint.activate([
140-
titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 18),
141-
titleLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
142-
timerLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 10),
143-
timerLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
144-
stopButton.topAnchor.constraint(equalTo: timerLabel.bottomAnchor, constant: 18),
145-
stopButton.centerXAnchor.constraint(equalTo: centerXAnchor),
146-
stopButton.widthAnchor.constraint(equalToConstant: 120),
147-
stopButton.heightAnchor.constraint(equalToConstant: 36),
148-
stopButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -18)
149-
])
150-
}
151-
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
152-
@objc func stopButtonPressed() { stopAction?() }
153-
func setTime(_ seconds: Int) {
154-
let h = seconds / 3600, m = (seconds % 3600) / 60, s = seconds % 60
155-
timerLabel.stringValue = String(format: "%02d:%02d:%02d", h, m, s)
156-
}
157-
}
158-
var studyPanel: StudyPanelView?
159-
var studyTimerSeconds: Int = 0
160-
var studyPanelTimer: Timer?
16191

16292
// 摸头动画循环计数
16393
var touchHeadLoopCount = 0
@@ -166,15 +96,15 @@ class AnimePlayer{
16696
// AC帧专属慢速因子
16797
let acFrameSlowFactor = 1// 可根据需要调整倍数
16898

99+
convenience init(imageView: NSImageView?, vPet: VPet?, timerLabel: NSTextField?){
100+
// 直接传入viewcontroller中创建好的timerLabel。为了减少代码改动使用了这种convenience init写法。
101+
self.init(imageView, vPet);
102+
self.timerLabel = timerLabel!
103+
}
104+
169105
init(_ ImageView: NSImageView?, _ VPET: VPet? = nil) {
170106
self.ImageView = ImageView
171107
self.VPET = VPET
172-
//文件读取所有动画
173-
//
174-
// guard let assetsURL = Bundle.main.resourceURL?.appendingPathComponent("0000_core/pet/vup") else{
175-
// print("strange... cant get the assets file.")
176-
// return;
177-
// }
178108

179109
// 从内置资源文件中读取动画
180110
let assetsURL = Bundle.main.resourceURL?.appendingPathComponent("0000_core/pet/vup");
@@ -183,20 +113,7 @@ class AnimePlayer{
183113

184114
self.allAnimes = FileSniffer(assetsURL!.path).sniff()
185115
animeInfoList = AnimeInfoList(Array(allAnimes.keys))
186-
// 自动启动学习计时器
187-
startStudyTimer()
188116

189-
// 添加计时器Label到ImageView的superview
190-
if let superview = ImageView?.superview {
191-
timerLabel.translatesAutoresizingMaskIntoConstraints = false
192-
superview.addSubview(timerLabel)
193-
NSLayoutConstraint.activate([
194-
timerLabel.centerXAnchor.constraint(equalTo: ImageView!.centerXAnchor),
195-
timerLabel.topAnchor.constraint(equalTo: ImageView!.topAnchor, constant: 220), // 下移到面板数字区域
196-
timerLabel.widthAnchor.constraint(equalToConstant: 220),
197-
timerLabel.heightAnchor.constraint(equalToConstant: 60)
198-
])
199-
}
200117
}
201118

202119
func setImageView(_ ImageView:NSImageView){self.ImageView = ImageView}
@@ -532,30 +449,6 @@ class AnimePlayer{
532449
}
533450
}
534451

535-
func startStudyTimer() {
536-
studyTimer?.invalidate()
537-
studySecondsElapsed = 0
538-
// 使用main线程RunLoop的.common模式注册Timer
539-
studyTimer = Timer(timeInterval: 1.0, target: self, selector: #selector(updateStudyTimer), userInfo: nil, repeats: true)
540-
if let timer = studyTimer {
541-
RunLoop.main.add(timer, forMode: .common)
542-
}
543-
print("学习计时器已启动")
544-
}
545-
546-
func stopStudyTimer() {
547-
studyTimer?.invalidate()
548-
studyTimer = nil
549-
}
550-
551-
@objc func updateStudyTimer() {
552-
studySecondsElapsed += 1
553-
let hours = studySecondsElapsed / 3600
554-
let minutes = (studySecondsElapsed % 3600) / 60
555-
let seconds = studySecondsElapsed % 60
556-
print(String(format: "学习计时:%02d:%02d:%02d", hours, minutes, seconds))
557-
}
558-
559452
// 启动计时器
560453
func startWorkTimer() {
561454
workStartTime = Date()
@@ -582,35 +475,4 @@ class AnimePlayer{
582475
let seconds = interval % 60
583476
timerLabel.stringValue = String(format: "%02d:%02d:%02d", hours, minutes, seconds)
584477
}
585-
586-
func showStudyPanel() {
587-
guard let superview = ImageView?.superview else { return }
588-
if studyPanel == nil {
589-
let panel = StudyPanelView(frame: NSRect(x: 0, y: 0, width: 260, height: 180))
590-
panel.stopAction = { [weak self] in self?.hideStudyPanel() }
591-
studyPanel = panel
592-
}
593-
guard let panel = studyPanel else { return }
594-
if panel.superview == nil { superview.addSubview(panel) }
595-
panel.translatesAutoresizingMaskIntoConstraints = false
596-
NSLayoutConstraint.activate([
597-
panel.centerXAnchor.constraint(equalTo: ImageView.centerXAnchor),
598-
panel.centerYAnchor.constraint(equalTo: ImageView.centerYAnchor, constant: 60),
599-
panel.widthAnchor.constraint(equalToConstant: 260),
600-
panel.heightAnchor.constraint(equalToConstant: 180)
601-
])
602-
panel.setTime(0)
603-
panel.isHidden = false
604-
studyTimerSeconds = 0
605-
studyPanelTimer?.invalidate()
606-
studyPanelTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
607-
guard let self = self else { return }
608-
self.studyTimerSeconds += 1
609-
self.studyPanel?.setTime(self.studyTimerSeconds)
610-
}
611-
}
612-
func hideStudyPanel() {
613-
studyPanelTimer?.invalidate()
614-
studyPanel?.isHidden = true
615-
}
616478
}

VPet-Mac/WindowController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class WindowController: NSWindowController,NSWindowDelegate {
2929
window?.setContentSize(NSSize(width: 300, height: 300))
3030

3131
// 初始化 AnimePlayer
32-
let player = AnimePlayer(viewcontroller.imagev, nil)
32+
let player = AnimePlayer(imageView: viewcontroller.imagev, vPet: nil, timerLabel: viewcontroller.timerLabel)
3333
viewcontroller.setAnimePlayer(player)
3434

3535
// 初始化VPET

0 commit comments

Comments
 (0)