问题描述
我尝试在iPhone中基于手电筒/闪光灯编写小型程序.现在我想添加SOS信号,但我不知道该怎么做.在此代码中,当我启动程序时,程序将每0.2秒打开和关闭我的LED一次.但我不知道如何在SOS信号中执行此操作.并且当用户单击SOS ON并单击SOS OFF时,led应立即关闭.我需要运行一些线程吗?还是在NSTimer上?
I tried write small program base on torch/flash in iPhones. Now i want add SOS signal but i have no idea how should do this. In this code when i start program will switch on and off my LED every 0.2 sec. But i don't know how to do this in SOS signal. And when user click SOS ON and click SOS OFF led should be off immediately. Do i need running some Thread ? or on 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()
}
}
推荐答案
有很多方法可以实现它.创建时间间隔序列,例如在开/关之间切换.代码注释.
Many ways to achieve it. Create sequence of time intervals and alternate between on/off for example. Comments in code.
///
/// 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()
}
}
这篇关于如何在Swift 2的SOS中创建闪光灯LED?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!