Skip to content

Commit 9e266f1

Browse files
Merge pull request bastienFalcou#41 from HanSJin/master
Add `meteringLevelBarSingleStick` design option.
2 parents 9376fe0 + b41c605 commit 9e266f1

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ self.audioVisualizationView.meteringLevelBarInterItem = 1.0
7373
self.audioVisualizationView.meteringLevelBarCornerRadius = 0.0
7474
```
7575

76+
You can change style of level bar to single:
77+
```swift
78+
self.audioVisualizationView.meteringLevelBarSingleStick = true
79+
```
80+
7681
You can change grandient start and end color:
7782

7883
```swift

SoundWave/Classes/AudioVisualizationView.swift

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ public class AudioVisualizationView: BaseNibView {
1313
case read
1414
case write
1515
}
16+
17+
private enum LevelBarType {
18+
case upper
19+
case lower
20+
case single
21+
}
1622

1723
@IBInspectable public var meteringLevelBarWidth: CGFloat = 3.0 {
1824
didSet {
@@ -29,6 +35,11 @@ public class AudioVisualizationView: BaseNibView {
2935
self.setNeedsDisplay()
3036
}
3137
}
38+
@IBInspectable public var meteringLevelBarSingleStick: Bool = false {
39+
didSet {
40+
self.setNeedsDisplay()
41+
}
42+
}
3243

3344
public var audioVisualizationMode: AudioVisualizationMode = .read
3445

@@ -294,27 +305,37 @@ public class AudioVisualizationView: BaseNibView {
294305
let offset = max(self.currentMeteringLevelsArray.count - self.maximumNumberBars, 0)
295306

296307
for index in offset..<self.currentMeteringLevelsArray.count {
297-
self.drawBar(index - offset, meteringLevelIndex: index, isUpperBar: true, context: context)
298-
self.drawBar(index - offset, meteringLevelIndex: index, isUpperBar: false, context: context)
308+
if self.meteringLevelBarSingleStick {
309+
self.drawBar(index - offset, meteringLevelIndex: index, levelBarType: .single, context: context)
310+
} else {
311+
self.drawBar(index - offset, meteringLevelIndex: index, levelBarType: .upper, context: context)
312+
self.drawBar(index - offset, meteringLevelIndex: index, levelBarType: .lower, context: context)
313+
}
299314
}
300315
}
301316

302-
private func drawBar(_ barIndex: Int, meteringLevelIndex: Int, isUpperBar: Bool, context: CGContext) {
317+
private func drawBar(_ barIndex: Int, meteringLevelIndex: Int, levelBarType: LevelBarType, context: CGContext) {
303318
context.saveGState()
304319

305-
var barPath: UIBezierPath!
320+
var barRect: CGRect
306321

307322
let xPointForMeteringLevel = self.xPointForMeteringLevel(barIndex)
308323
let heightForMeteringLevel = self.heightForMeteringLevel(self.currentMeteringLevelsArray[meteringLevelIndex])
309324

310-
if isUpperBar {
311-
barPath = UIBezierPath(roundedRect: CGRect(x: xPointForMeteringLevel, y: self.centerY - heightForMeteringLevel,
312-
width: self.meteringLevelBarWidth, height: heightForMeteringLevel), cornerRadius: self.meteringLevelBarCornerRadius)
313-
} else {
314-
barPath = UIBezierPath(roundedRect: CGRect(x: xPointForMeteringLevel, y: self.centerY, width: self.meteringLevelBarWidth,
315-
height: heightForMeteringLevel), cornerRadius: self.meteringLevelBarCornerRadius)
316-
}
317-
325+
switch levelBarType {
326+
case .upper:
327+
barRect = CGRect(x: xPointForMeteringLevel, y: self.centerY - heightForMeteringLevel,
328+
width: self.meteringLevelBarWidth, height: heightForMeteringLevel)
329+
case .lower:
330+
barRect = CGRect(x: xPointForMeteringLevel, y: self.centerY, width: self.meteringLevelBarWidth,
331+
height: heightForMeteringLevel)
332+
case .single:
333+
barRect = CGRect(x: xPointForMeteringLevel, y: self.centerY - heightForMeteringLevel,
334+
width: self.meteringLevelBarWidth, height: heightForMeteringLevel * 2)
335+
}
336+
337+
let barPath: UIBezierPath = UIBezierPath(roundedRect: barRect, cornerRadius: self.meteringLevelBarCornerRadius)
338+
318339
UIColor.black.set()
319340
barPath.fill()
320341

0 commit comments

Comments
 (0)