如何在swift 2上的SOS中创建闪光LED?

Wra*_*ith 1 iphone ios swift2

我尝试在iPhone上使用手电筒/闪光灯编写小程序.现在我想添加SOS信号,但我不知道应该怎么做.在此代码中,当我启动程序时,每0.2秒打开和关闭我的LED.但我不知道如何在SOS信号中做到这一点.当用户单击SOS ON并单击SOS OFF时,应立即关闭led.我需要运行一些线程吗?或者在NSTimer上?

class Sos {
    var timer1 = NSTimer()
    var timer2 = NSTimer()
    var volume: Float = 0.1
    let flashLight = FlashLight()

    func start() {

        self.timer1 = NSTimer.scheduledTimerWithTimeInterval(0.2,
            target: self,
            selector: Selector("switchON"),
            userInfo: nil,
            repeats: true)

        self.timer2 = NSTimer.scheduledTimerWithTimeInterval(0.4,
            target: self,
            selector: Selector("switchOFF"),
            userInfo: nil,
            repeats: true)
    }

    func stop() {
        timer1.invalidate()
        timer2.invalidate()
        flashLight.switchOFF()
    }


    @objc func switchON() {
        flashLight.switchON(self.volume)
    }

    @objc func switchOFF() {
        flashLight.switchOFF()
    }

    deinit {
        self.timer1.invalidate()
        self.timer2.invalidate()
    }

}
Run Code Online (Sandbox Code Playgroud)

zrz*_*zka 5

实现它的方法很多.例如,创建时间间隔序列并在开/关之间切换.代码中的评论.

///
/// SOS sequence: ...---...
///
/// . short
/// - long
///
class SOS {
  /// Short signal duration (LED on)
  private static let shortInterval = 0.2
  /// Long signal duration (LED on)
  private static let longInterval = 0.4
  /// Pause between signals (LED off)
  private static let pauseInterval = 0.2
  /// Pause between the whole SOS sequences (LED off)
  private static let sequencePauseInterval = 2.0

  /**
    When the SOS sequence is started flashlight is on. Thus
    the first time interval is for the short signal. Then pause,
    then short, ...

    See `timerTick()`, it alternates flashlight status (on/off) based
    on the current index in this sequence.
  */
  private let sequenceIntervals = [
    shortInterval, pauseInterval, shortInterval, pauseInterval, shortInterval, pauseInterval,
    longInterval, pauseInterval, longInterval, pauseInterval, longInterval, pauseInterval,
    shortInterval, pauseInterval, shortInterval, pauseInterval, shortInterval, sequencePauseInterval
  ]

  /// Current index in the SOS `sequence`
  private var index: Int = 0

  /// Non repeatable timer, because time interval varies
  private weak var timer: NSTimer?

  /**
    Put your `Flashlight()` calls inside this function.

    - parameter on: pass `true` to turn it on or `false` to turn it off
  */
  private func turnFlashlight(on on: Bool) {
    // if on == true  -> turn it on
    // if on == false -> turn it off
    print(on ? "ON" : "OFF")
  }

  private func scheduleTimer() {
    timer = NSTimer.scheduledTimerWithTimeInterval(sequenceIntervals[index],
      target: self, selector: "timerTick",
      userInfo: nil, repeats: false)
  }

  @objc private func timerTick() {
    // Increase sequence index, at the end?
    if ++index == sequenceIntervals.count {
      // Start from the beginning
      index = 0
    }
    // Alternate flashlight status based on current index
    // index % 2 == 0 -> is index even number? 0, 2, 4, 6, ...
    turnFlashlight(on: index % 2 == 0)
    scheduleTimer()
  }

  func start() {
    index = 0
    turnFlashlight(on: true)
    scheduleTimer()
  }

  func stop() {
    timer?.invalidate()
    turnFlashlight(on: false)
  }

  deinit {
    timer?.invalidate()
  }
}
Run Code Online (Sandbox Code Playgroud)