Sir Godart
Sir Godart

Reputation: 33

Using a function() variable in a different Class - Swift

How can I use the value of getLevel() inside the customView class? This function is linked with an NSTimer. The values are the decibel values of an mp3 file, which I want to use inside customView to sync the decibels values with an animation

class customView: UIView {

// get getLevels() value

}


class ViewController: UIViewController, AVAudioPlayerDelegate {


override func viewDidLoad() {
    super.viewDidLoad()



   var bundle = NSString(string: NSBundle.mainBundle().pathForResource("guitar", ofType: "mp3")!)
   var error: NSError? = nil
   player = AVAudioPlayer(contentsOfURL: NSURL(string: bundle), error: &error)
   player.delegate = self
   player.volume = 1.0
   player.meteringEnabled = true
   player.play()



    var timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("getLevel"), userInfo: nil, repeats: true)

}


func getLevel() {

    if (player.playing == true) {

        player.updateMeters()
        var dblevel = player.averagePowerForChannel(1)
        var percentage = CGFloat( pow(10, (0.05 * dblevel)) * 100)

        // This prints out the the decibel levels from 0.0 to 100.0 every 0.1 sec
        println(percentage)


    }

}

The variable I will need is 'percentage'. What are my options to do this? Thanks, Godart.

Upvotes: 0

Views: 293

Answers (2)

ngoue
ngoue

Reputation: 1055

Following the Model-View-Controller design pattern, your ViewController should be responsible for handling model information and passing it to your view to display the info to the user. Therefore, you should create a variable in your view controller class to store the CustomView:

class ViewController: UIViewController, AVAudioPlayerDelegate {


override func viewDidLoad() {
    super.viewDidLoad()


   // new var to store the customView if you're using Storyboards
   @IBOutlet var customView: CustomView!
   // new var to store the customView without using Storyboards
   var customView: CustomView?

   var bundle = NSString(string: NSBundle.mainBundle().pathForResource("guitar", ofType: "mp3")!)
   var error: NSError? = nil
   player = AVAudioPlayer(contentsOfURL: NSURL(string: bundle), error: &error)
   player.delegate = self
   player.volume = 1.0
   player.meteringEnabled = true
   player.play()



    var timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("getLevel"), userInfo: nil, repeats: true)

}

This will allow you to update the values in your CustomView directly from your controller and you can use the getLevel() method to do so. If you only need the percentage value from within the function, then I suggest you calculate the percentage in its own method so you can use the percentage in multiple places without repeating code.

func calculatePercentage() -> CGFloat {

  let dblevel = player.averagePowerForChannel(1)

  return CGFloat(pow(10, (0.05 * dblevel)) * 100)

}

Then you can use the method instead of typing the same code over and over.

func getLevel() {

  if (player.playing == true) {

    player.updateMeters()
    let percentage = calculatePercentage()

    // This prints out the decibel levels from 0.0 to 100.0 every 0.1 sec
    println(percentage)

  }

}

Somewhere in your view controller when you're ready to update your view, you can pass the percentage like this:

self.customView.updateView(percentage: calculatePercentage())

Upvotes: 0

Daniel T.
Daniel T.

Reputation: 33967

Comments in the code should be all you need to understand this:

class CustomView: UIView {
    var percentage: CGFloat = 0.0 {
        didSet {
            setNeedsDisplay() // this will cause the system to tell the view to draw itself.
        }
    }

    func drawRect(rect: Rect) {
        // draw yourself based on percentage
    }
}

class ViewController: ... {
    func getLevel() {

        if (player.playing == true) {

            player.updateMeters()
            var dblevel = player.averagePowerForChannel(1)
            var percentage = CGFloat( pow(10, (0.05 * dblevel)) * 100)

            myCustomView.percentage = percentage // this is how to pass the information.
        }
    }
}

Upvotes: 1

Related Questions