@@ -36,15 +36,28 @@ protocol IHWaveFormViewDelegate : class {
3636
3737class IHWaveFormView : UIView , AVAudioPlayerDelegate {
3838 private var player : AVAudioPlayer !
39+ fileprivate let centerLineView = UIView ( )
3940 private var dataArray : [ Float ] = [ ]
4041 private var totalCount : Int = 0
4142 private var xPoint : CGFloat = 0.0
4243 private var gameTimer : Timer !
43- private var internalLineWidth : CGFloat !
44- private var internalLineSeperation : CGFloat !
45- weak var delegate : IHWaveFormViewDelegate ?
46- weak var dataSource : IHWaveFormViewDataSource ?
44+ private var internalLineWidth : CGFloat = 2.0
45+ private var internalLineSeperation : CGFloat = 1.0
46+ fileprivate var width : CGFloat ?
4747
48+ private var urlToPlay : URL {
49+ return ( dataSource? . urlToPlay ( ) ) !
50+ }
51+
52+ weak var delegate : IHWaveFormViewDelegate ?
53+ weak var dataSource : IHWaveFormViewDataSource ? {
54+ didSet {
55+ if ( dataSource? . urlToPlay ( ) != nil ) {
56+ self . getPath ( url: ( dataSource? . urlToPlay ( ) ) !)
57+ }
58+ }
59+ }
60+
4861 required init ? ( coder aDecoder: NSCoder ) {
4962 super. init ( coder: aDecoder)
5063 xPoint = 0.0
@@ -55,12 +68,9 @@ class IHWaveFormView: UIView, AVAudioPlayerDelegate {
5568 xPoint = 0.0
5669 }
5770
58- fileprivate var width : CGFloat ?
59-
6071 override func draw( _ rect: CGRect ) {
6172 super. draw ( rect)
62- self . commonInit ( )
63- self . getPath ( url: urlToPlay!)
73+ commonInit ( )
6474 }
6575
6676 var orientationChangesCount : Int = 0
@@ -72,61 +82,53 @@ class IHWaveFormView: UIView, AVAudioPlayerDelegate {
7282 redrawView ( )
7383 }
7484
75- func eraseView( ) {
85+ private func eraseView( ) {
7686 for views in subviews {
7787 views. removeFromSuperview ( )
7888 }
7989 self . layer. sublayers? . forEach { $0. removeFromSuperlayer ( ) }
8090 }
8191
82- internal func audioPlayerDidFinishPlaying( _ player: AVAudioPlayer , successfully flag: Bool ) {
92+ func audioPlayerDidFinishPlaying( _ player: AVAudioPlayer , successfully flag: Bool ) {
8393 self . gameTimer. invalidate ( )
8494 delegate? . didFinishPlayBack ( )
8595 }
8696
87- var centerLineView : UIView ?
8897
89- func addCentreLine( ) {
90- centerLineView = UIView . init ( frame: CGRect . init ( x: 0 , y: self . frame. size. height / 2.0 , width: self . frame. size. width, height: 1 ) )
91- centerLineView? . backgroundColor = . black
92- self . addSubview ( centerLineView! )
98+ private func addCentreLine( ) {
99+ centerLineView. frame = CGRect . init ( x: 0 , y: self . frame. size. height / 2.0 , width: self . frame. size. width, height: 1 )
100+ centerLineView. backgroundColor = . black
101+ self . addSubview ( centerLineView)
93102 }
94103
95- func invertColor( _ color : UIColor ) -> UIColor {
104+ private func invertColor( _ color : UIColor ) -> UIColor {
96105 var r : CGFloat = 0 , g : CGFloat = 0 , b : CGFloat = 0 , a : CGFloat = 0
97106 self . backgroundColor? . getRed ( & r, green: & g, blue: & b, alpha: & a)
98107 let lineColor = UIColor . init ( red: self . invertB ( r) , green: self . invertB ( g) , blue: self . invert ( b) , alpha: a)
99108 return lineColor
100109 }
101110
102- func invertB( _ val : CGFloat ) -> CGFloat {
111+ private func invertB( _ val : CGFloat ) -> CGFloat {
103112 return ( 1.0 - val) * 0.9
104113 }
105114
106- func invert( _ val : CGFloat ) -> CGFloat {
115+ private func invert( _ val : CGFloat ) -> CGFloat {
107116 if ( val > 0.6 && val < 0.90 ) {
108117 return 0.6 + 0.1
109118 }
110119 return ( 1.0 - val)
111120 }
112121
113- private var urlToPlay : URL ?
114-
115- func setUpView( urlToPlay : URL , lineWith : CGFloat ? , lineSeperation : CGFloat ? ) {
116- if ( lineWith != nil ) {
117- internalLineWidth = lineWith
118- }
119- if ( lineSeperation != nil ) {
120- internalLineSeperation = lineSeperation
121- }
122- self . urlToPlay = urlToPlay
123- }
124-
125122 func commonInit( ) {
126- internalLineWidth = 2.0
127- internalLineSeperation = 1.0
128- // self.addOverlayLabels()
129123 self . addCentreLine ( )
124+ guard let lineWidth = dataSource? . lineWidth ? ( ) else {
125+ return
126+ }
127+ guard let lineSeperation = dataSource? . lineSeperation ? ( ) else {
128+ return
129+ }
130+ internalLineWidth = lineWidth
131+ internalLineSeperation = lineSeperation
130132 }
131133
132134 private func getPath( url : URL ) {
@@ -207,10 +209,9 @@ class IHWaveFormView: UIView, AVAudioPlayerDelegate {
207209 private func redrawView( ) {
208210 eraseView ( )
209211 self . commonInit ( )
210- self . getPath ( url: urlToPlay! )
212+ self . getPath ( url: urlToPlay)
211213 }
212214
213-
214215 private func generatePoints1( dBVal : String ) {
215216 let aPath = UIBezierPath ( )
216217 let floatVal : Float = Float ( dBVal) !
0 commit comments