在另一个类中使用function()变量 - Swift

时间:2014-12-30 08:43:03

标签: swift global-variables

如何在customView类中使用getLevel()的值?此功能与NSTimer链接。值是mp3文件的分贝值,我想在customView中使用它来将分贝值与动画同步

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)


    }

}

我需要的变量是'百分比'。我有什么选择呢? 谢谢Godart。

2 个答案:

答案 0 :(得分:1)

代码中的注释应该是您理解这一点所需的全部内容:

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.
        }
    }
}

答案 1 :(得分:0)

遵循模型 - 视图 - 控制器设计模式,您的ViewController应负责处理模型信息并将其传递给您的视图以向用户显示信息。因此,您应该在视图控制器类中创建一个变量来存储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)

}

这将允许您直接从控制器更新CustomView中的值,您可以使用getLevel()方法执行此操作。如果您只需要函数中的percentage值,那么我建议您使用自己的方法计算百分比,这样您就可以在多个位置使用百分比而无需重复代码。

func calculatePercentage() -> CGFloat {

  let dblevel = player.averagePowerForChannel(1)

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

}

然后你可以使用该方法,而不是一遍又一遍地输入相同的代码。

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)

  }

}

当您准备好更新视图时视图控制器中的某个位置,您可以传递这样的百分比:

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