努力引用变量(Swift)

时间:2018-10-17 04:07:53

标签: ios swift function variables view

背景: 我正在尝试使用BAFluidView为波浪设置动画。我的目标很简单:轻按按钮时启动动画,再次轻按则停止动画。吊舱(上方链接)提供了用于管理此代码的所有代码。

让我知道,我是新来的。请帮助我学习!


我的奋斗:

我需要为此波创建一个视图。我将其放入名为“ WaveContainer”的函数中。看起来像这样:

func WaveAnimation() {

        let wave = BAFluidView(frame: self.view.frame, startElevation: 0.02)!

        wave.maxAmplitude = 10
        wave.minAmplitude = 8
        wave.fillDuration = 50
        wave.fill(to: 0.95)
        wave.fillAutoReverse = false
        wave.fillColor = UIColor.blue

        waveView.addSubview(wave)

    }

然后我在ViewDidAppear函数中将此称为。当然可以!我可以看到波浪,它在挥舞。真好

当然,我无法在其他任何地方调用wave常量! 例如,如果我想按一下按钮停止/开始波浪运动?

如果我尝试将wave常量从该函数移出到ViewDidLoadViewDidAppear中,则无法访问...self.view.frame,并且wave不会出现在屏幕上。

我的问题:

  1. 我应该如何构造此代码,以便可以从几个不同的函数引用wave常量?
  2. 当我不在ViewDidLoad或其他基于视图的函数中时,如何为view引用wave

非常感谢您的帮助!


我的代码:

import UIKit
import BAFluidView

class ViewController: UIViewController {

    // OUTLETS: 

    @IBOutlet weak var waveView: UIView!



    // ACTIONS: 

    @IBAction func WaveButton(_ sender: UIButton) {
       // If the user taps this button and the waveHasStarted variable is equal to false, flip it to true.
       if waveHasStarted == false {
            print("Start the wave with a press.")
            startWave = true
            waveHasStarted = true
            waveHasBeenStopped = false
        } else {
            print("Stop the wave with a press.")
            startWave = false
            waveHasStarted = false
            waveHasBeenStopped = true
        }

    }


    // VARIABLES:

    var waveHasStarted = false
    var startWave = false
    var waveHasBeenStopped = true



    override func viewDidLoad() {
        super.viewDidLoad()

    }


    override func viewDidAppear(_ animated: Bool) {}



    // FUNCTIONS:

    func WaveAnimation() {

        let wave = BAFluidView(frame: self.view.frame, startElevation: 0.02)!

        wave.maxAmplitude = 10
        wave.minAmplitude = 8
        wave.fillDuration = 50
        wave.fill(to: 0.95)
        wave.fillAutoReverse = false
        wave.fillColor = UIColor.blue


        // If the variable above has been flipped to "true," start the animation...
        if startWave == true {
            print("Start that wave animation")
            wave.startAnimation()
        } else {
            // If not, stop it or do nothing.
            print("Stop that wave animation")
            wave.stopAnimation()
            wave.keepStationary()
        }

        waveView.addSubview(wave)

    }


}

1 个答案:

答案 0 :(得分:2)

使用lazy初始化并获得class级访问权限,

lazy var fluidView: BAFluidView = {
       let wave = BAFluidView(frame: self.view.frame, startElevation: 0.02)!
       wave.maxAmplitude = 10
       wave.minAmplitude = 8
       wave.fillDuration = 50
       wave.fill(to: 0.95)
       wave.fillAutoReverse = false
       wave.fillColor = UIColor.blue
       return wave
}()

现在您可以在任何地方addstartstop动画了,

waveView.addSubview(fluidView)
fluidView.startAnimation()
fluidView.stopAnimation()