@@ -84,80 +84,10 @@ class AnimePlayer{
84
84
var studySecondsElapsed : Int = 0
85
85
86
86
// 计时器数字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 !
100
88
var workStartTime : Date ?
101
89
var workTimer : Timer ?
102
90
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 ?
161
91
162
92
// 摸头动画循环计数
163
93
var touchHeadLoopCount = 0
@@ -166,15 +96,15 @@ class AnimePlayer{
166
96
// AC帧专属慢速因子
167
97
let acFrameSlowFactor = 1 // 可根据需要调整倍数
168
98
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
+
169
105
init ( _ ImageView: NSImageView ? , _ VPET: VPet ? = nil ) {
170
106
self . ImageView = ImageView
171
107
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
- // }
178
108
179
109
// 从内置资源文件中读取动画
180
110
let assetsURL = Bundle . main. resourceURL? . appendingPathComponent ( " 0000_core/pet/vup " ) ;
@@ -183,20 +113,7 @@ class AnimePlayer{
183
113
184
114
self . allAnimes = FileSniffer ( assetsURL!. path) . sniff ( )
185
115
animeInfoList = AnimeInfoList ( Array ( allAnimes. keys) )
186
- // 自动启动学习计时器
187
- startStudyTimer ( )
188
116
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
- }
200
117
}
201
118
202
119
func setImageView( _ ImageView: NSImageView ) { self . ImageView = ImageView}
@@ -532,30 +449,6 @@ class AnimePlayer{
532
449
}
533
450
}
534
451
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
-
559
452
// 启动计时器
560
453
func startWorkTimer( ) {
561
454
workStartTime = Date ( )
@@ -582,35 +475,4 @@ class AnimePlayer{
582
475
let seconds = interval % 60
583
476
timerLabel. stringValue = String ( format: " %02d:%02d:%02d " , hours, minutes, seconds)
584
477
}
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
- }
616
478
}
0 commit comments