ios - How to create flash LED in SOS on swift 2? -


i tried write small program base on torch/flash in iphones. want add sos signal have no idea how should this. in code when start program switch on , off led every 0.2 sec. don't know how in sos signal. , when user click sos on , click sos off led should off immediately. need running 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 achieve it. create sequence of time intervals , alternate between on/off 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 whole sos sequences (led off)   private static let sequencepauseinterval = 2.0    /**     when sos sequence started flashlight on.     first time interval short signal. pause,     short, ...      see `timertick()`, alternates flashlight status (on/off) based     on current index in 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 sos `sequence`   private var index: int = 0    /// non repeatable timer, because time interval varies   private weak var timer: nstimer?    /**     put `flashlight()` calls inside function.      - parameter on: pass `true` turn on or `false` turn off   */   private func turnflashlight(on on: bool) {     // if on == true  -> turn on     // if on == false -> turn 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, @ end?     if ++index == sequenceintervals.count {       // start beginning       index = 0     }     // alternate flashlight status based on current index     // index % 2 == 0 -> index 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()   } } 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -