Skip to content

Commit c479366

Browse files
committed
Change fonts and colors of pitch animation
1 parent 1c658be commit c479366

File tree

1 file changed

+21
-87
lines changed

1 file changed

+21
-87
lines changed

ScienceJournal/UI/PitchSensorAnimationView.swift

+21-87
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,21 @@ import UIKit
1919

2020
import MaterialComponents.MaterialTypography
2121

22-
fileprivateextensionMDCTypography{
23-
24-
staticfunc boldFont(withSize size:CGFloat)->UIFont{
25-
// This font should load unless something is wrong with Material's fonts.
26-
returnMDCTypography.fontLoader().boldFont!(ofSize: size)
27-
}
22+
fileprivateextensionArduinoTypography{
2823

2924
staticvarfontName:String{
3025
// Arbitrary size to get the font name.
31-
returnboldFont(withSize:10).fontName
26+
returnmonoBoldFont(forSize:10).fontName
3227
}
3328

3429
}
3530

36-
// swiftlint:disable type_body_length
3731
/// Animation view for the pitch sensor.
3832
classPitchSensorAnimationView:SensorAnimationView{
3933

4034
// MARK: - Nested
4135

4236
privateenumMetrics{
43-
staticletbackgroundColorValueBlueHigh:CGFloat=175
44-
staticletbackgroundColorValueBlueLow:CGFloat=248
45-
staticletbackgroundColorValueGreenHigh:CGFloat=97
46-
staticletbackgroundColorValueGreenLow:CGFloat=202
47-
staticletbackgroundColorValueRedHigh:CGFloat=13
48-
staticletbackgroundColorValueRedLow:CGFloat=113
49-
staticletbackgroundRadiusRatio:CGFloat=0.38
5037
staticletdotAngleTop=CGFloat(Double.pi /2)
5138
staticletdotEllipseRadiusRatio:CGFloat=0.44
5239
staticletdotRadiusRatio:CGFloat=0.05
@@ -55,21 +42,16 @@ class PitchSensorAnimationView: SensorAnimationView {
5542
staticletnoteFontSizeRatio:CGFloat=0.48
5643
staticletnoteRatioX:CGFloat=0.42
5744
staticletnoteRatioY:CGFloat=0.46
58-
staticletnoteLeftTextColor=
59-
UIColor(red:238/255, green:238/255, blue:238/255, alpha:1).cgColor
60-
staticletnoteRightTextColor=
61-
UIColor(red:220/255, green:220/255, blue:220/255, alpha:1).cgColor
45+
staticletnoteTextColor=UIColor.white.cgColor
6246
staticletnumberOfPianoKeys=88
6347
staticletoctaveFontSizeRatio:CGFloat=0.2
6448
staticletoctaveRatioX:CGFloat=0.67
6549
staticletoctaveRatioY:CGFloat=0.67
66-
staticletoctaveTextColor=
67-
UIColor(red:220/255, green:220/255, blue:220/255, alpha:1).cgColor
68-
staticletsignFontSizeRatio:CGFloat=0.25
69-
staticletsignRatioX:CGFloat=0.62
50+
staticletoctaveTextColor=UIColor.white.cgColor
51+
staticletsignFontSizeRatio:CGFloat=0.3
52+
staticletsignRatioX:CGFloat=0.68
7053
staticletsignRatioY:CGFloat=0.43
71-
staticletsignTextColor=
72-
UIColor(red:220/255, green:220/255, blue:220/255, alpha:1).cgColor
54+
staticletsignTextColor=UIColor.white.cgColor
7355
staticlettextShadowColor=UIColor(white:0, alpha:0.61).cgColor
7456
staticlettextShadowOffset=CGSize(width:1, height:2)
7557
staticlettextShadowRadius:CGFloat=4
@@ -91,8 +73,7 @@ class PitchSensorAnimationView: SensorAnimationView {
9173

9274
privateletbackgroundShapeLayer=CAShapeLayer()
9375
privateletdotShapeLayer=CAShapeLayer()
94-
privateletnoteLeftTextLayer=CATextLayer()
95-
privateletnoteRightTextLayer=CATextLayer()
76+
privateletnoteTextLayer=CATextLayer()
9677
privateletsignTextLayer=CATextLayer()
9778
privateletoctaveTextLayer=CATextLayer()
9879

@@ -201,14 +182,6 @@ class PitchSensorAnimationView: SensorAnimationView {
201182
overridefunc layoutSubviews(){
202183
super.layoutSubviews()
203184

204-
// Background shape layer.
205-
letbackgroundShapeRadius= bounds.width * Metrics.backgroundRadiusRatio
206-
letbackgroundShapeRect=CGRect(x: bounds.midX - backgroundShapeRadius,
207-
y: bounds.midY - backgroundShapeRadius,
208-
width: backgroundShapeRadius *2,
209-
height: backgroundShapeRadius *2)
210-
backgroundShapeLayer.path =UIBezierPath(ovalIn: backgroundShapeRect).cgPath
211-
212185
// Dot shape layer will be at a point on an invisible ellipse.
213186
letellipseRadius= bounds.width * Metrics.dotEllipseRadiusRatio
214187
letdotX= bounds.midX + ellipseRadius * cos(angleOfDot)
@@ -224,12 +197,11 @@ class PitchSensorAnimationView: SensorAnimationView {
224197
iflet musicalNote = musicalNote {
225198
// Letter
226199
letnoteFontSize=floor(bounds.height * Metrics.noteFontSizeRatio)
227-
noteLeftTextLayer.fontSize = noteFontSize
228-
noteRightTextLayer.fontSize = noteFontSize
200+
noteTextLayer.fontSize = noteFontSize
229201
letnoteSize= musicalNote.letter.boundingRect(
230202
with:.zero,
231203
options:[],
232-
attributes:[NSAttributedString.Key.font:MDCTypography.boldFont(withSize: noteFontSize)],
204+
attributes:[NSAttributedString.Key.font:ArduinoTypography.monoBoldFont(forSize: noteFontSize)],
233205
context:nil).size
234206
varnoteX:CGFloat
235207
varnoteY:CGFloat
@@ -242,18 +214,14 @@ class PitchSensorAnimationView: SensorAnimationView {
242214
}
243215

244216
// The note layer on the left is half width.
245-
noteLeftTextLayer.frame =CGRect(x:floor(noteX),
217+
noteTextLayer.frame =CGRect(x:floor(noteX),
246218
y:floor(noteY),
247-
width:ceil(noteSize.width/2),
219+
width:ceil(noteSize.width),
248220
height:ceil(noteSize.height))
249-
noteRightTextLayer.frame =CGRect(x:floor(noteX),
250-
y:floor(noteY),
251-
width:ceil(noteSize.width),
252-
height:ceil(noteSize.height))
253-
221+
254222
// Sign.
255223
signTextLayer.fontSize =floor(bounds.height * Metrics.signFontSizeRatio)
256-
letsignFont=MDCTypography.boldFont(withSize: signTextLayer.fontSize)
224+
letsignFont=ArduinoTypography.monoBoldFont(forSize: signTextLayer.fontSize)
257225
letsignSize= musicalNote.sign.boundingRect(
258226
with:.zero,
259227
options:[],
@@ -267,7 +235,7 @@ class PitchSensorAnimationView: SensorAnimationView {
267235

268236
// Octave
269237
octaveTextLayer.fontSize =floor(bounds.height * Metrics.octaveFontSizeRatio)
270-
letoctaveFont=MDCTypography.boldFont(withSize: octaveTextLayer.fontSize)
238+
letoctaveFont=ArduinoTypography.monoBoldFont(forSize: octaveTextLayer.fontSize)
271239
letoctaveSize=
272240
musicalNote.octave.boundingRect(with:.zero,
273241
options:[],
@@ -287,17 +255,13 @@ class PitchSensorAnimationView: SensorAnimationView {
287255
// Store the level.
288256
self.level = level
289257

290-
// Set the fill color of the background shape layer.
291-
backgroundShapeLayer.fillColor =backgroundColor(forLevel: level).cgColor
292-
293258
// The the angle of the dot.
294259
letdifference=differenceBetween(pitch: value, andLevel: level)
295260
angleOfDot =CGFloat((1-2* difference)*(Double.pi /2))
296261

297262
// Set the musical note letter, sign and octave.
298263
letmusicalNote=musicalNotes[level]
299-
noteLeftTextLayer.string = musicalNote.letter
300-
noteRightTextLayer.string = musicalNote.letter
264+
noteTextLayer.string = musicalNote.letter
301265
signTextLayer.string = musicalNote.sign
302266
octaveTextLayer.string = musicalNote.octave
303267

@@ -327,29 +291,6 @@ class PitchSensorAnimationView: SensorAnimationView {
327291

328292
// MARK: - Private
329293

330-
privatefunc backgroundColor(forLevel level:Int)->UIColor{
331-
if level ==0{
332-
returnUIColor(red:Metrics.backgroundColorValueRedLow /255,
333-
green:Metrics.backgroundColorValueGreenLow /255,
334-
blue:Metrics.backgroundColorValueBlueLow /255, alpha:1)
335-
}elseif level == noteFrequencies.endIndex -1{
336-
returnUIColor(red:Metrics.backgroundColorValueRedHigh /255,
337-
green:Metrics.backgroundColorValueGreenHigh /255,
338-
blue:Metrics.backgroundColorValueBlueHigh /255, alpha:1)
339-
}else{
340-
letred=round(Metrics.backgroundColorValueRedLow +
341-
(Metrics.backgroundColorValueRedHigh - Metrics.backgroundColorValueRedLow)*
342-
CGFloat(level)/ CGFloat(noteFrequencies.endIndex -1))
343-
letgreen=round(Metrics.backgroundColorValueGreenLow +
344-
(Metrics.backgroundColorValueGreenHigh - Metrics.backgroundColorValueGreenLow)*
345-
CGFloat(level)/ CGFloat(noteFrequencies.endIndex -1))
346-
letblue=round(Metrics.backgroundColorValueBlueLow +
347-
(Metrics.backgroundColorValueBlueHigh - Metrics.backgroundColorValueBlueLow)*
348-
CGFloat(level)/ CGFloat(noteFrequencies.endIndex -1))
349-
returnUIColor(red: red /255, green: green /255, blue: blue /255, alpha:1)
350-
}
351-
}
352-
353294
privatefunc configureView(){
354295
isAccessibilityElement =true
355296

@@ -359,31 +300,24 @@ class PitchSensorAnimationView: SensorAnimationView {
359300
imageView.frame = bounds
360301
addSubview(imageView)
361302

362-
// Background shape layer.
363-
backgroundShapeLayer.fillColor =backgroundColor(forLevel:0).cgColor
364-
layer.addSublayer(backgroundShapeLayer)
365-
366303
// Dot shape layer.
367-
dotShapeLayer.fillColor =UIColor.red.cgColor
304+
dotShapeLayer.fillColor =ArduinoColorPalette.warningColor.cgColor
368305
layer.addSublayer(dotShapeLayer)
369306

370307
// Text layer common configuration.
371-
[noteRightTextLayer, noteLeftTextLayer, signTextLayer, octaveTextLayer].forEach{
308+
[noteTextLayer, signTextLayer, octaveTextLayer].forEach{
372309
$0.alignmentMode =.center
373310
$0.contentsScale =UIScreen.main.scale
374-
$0.font =CGFont(MDCTypography.fontName asCFString)
311+
$0.font =CGFont(ArduinoTypography.fontName asCFString)
375312
$0.shadowColor =Metrics.textShadowColor
376313
$0.shadowOffset =Metrics.textShadowOffset
377314
$0.shadowRadius =Metrics.textShadowRadius
378315
layer.addSublayer($0)
379316
}
380317

381-
// Right note.
382-
noteRightTextLayer.foregroundColor =Metrics.noteRightTextColor
383-
384318
// Left note.
385-
noteLeftTextLayer.foregroundColor =Metrics.noteLeftTextColor
386-
noteLeftTextLayer.masksToBounds =true
319+
noteTextLayer.foregroundColor =Metrics.noteTextColor
320+
noteTextLayer.masksToBounds =true
387321

388322
// Sign.
389323
signTextLayer.foregroundColor =Metrics.signTextColor

0 commit comments

Comments
 (0)
close