0

I want to make an iPhone app, programmed in Swift, that will change the flashlight between different intensities over time. The purpose of this is to replicate a flashlight with low battery.


I want it to be like a "timeline", where the intensity will fade between different intensities like this:

  • At 0 minutes: 100%
  • At 100 minutes: 0%

Now and then I would like the flashlight to flash a little to make it seem even more like a flashlight with low battery. It could be like this:

  • At 30 minutes: 70%
  • At 30 minutes and 1 second: 100%
  • At 30 minutes and 2 seconds: 70%
  • At 30 minutes and 3 seconds: 20%
  • At 30 minutes and 6 seconds: 70%

I have read about how to turn the flashlight on here, and how to make a timer (using NSTimer) here, but I can't figure out how to combine them in order to make the flashlight intensity change over time.

What would be the best way to accomplish this?

Thank you in advance!

Community
  • 1
  • 1
Thomas
  • 13
  • 6

1 Answers1

3

Use the function setTorchModeOnWithLevel to set the level, you can use a timer to set times when the level will change.

Here's an example of how to do this (implemented in the AppDelegate of a brand-new iOS Single View Application):

  var timer: NSTimer?

  func applicationDidBecomeActive(application: UIApplication) {
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "delayedAction", userInfo: nil, repeats: true)
  }

  let maxLightLevel = 5
  var lightLevel = 5

  func delayedAction() {
    guard let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
      else {
        timer?.invalidate()
        return
    }
    if let _ = try? device.lockForConfiguration() {
      defer { device.unlockForConfiguration() }
      if lightLevel == 0 {
        timer?.invalidate()
        device.torchMode = .Off
      }
      else {
        try! device.setTorchModeOnWithLevel(Float(lightLevel)/Float(maxLightLevel))
      }
      --lightLevel
    }
  }

This code is just a rough example and should have appropriate behaviors added to handle switching apps, thrown exceptions, and so on.

  • should always avoid try! and implement error handling do { try } catch let error as NSError { print(error.code) } – Leo Dabus Dec 28 '15 at 20:42
  • @LeoDabus Of course, this is just a quick example that left things out for brevity. By no means does it do complete error handling or handle all tricky areas. It just shows the basics of changing the light level with a timer. –  Dec 28 '15 at 20:45
  • Class Appdelegate has no initilalizers – Asim Khan Mar 09 '17 at 08:01
  • @AasimKhan Whether it does or does not have an initializer doesn't seem, to me, to directly affect the answer. Do you think differently? –  Mar 09 '17 at 14:48
  • Thanks for sharp reply, I was really needing it. Actually I'm still using this as you mentioned but finally it gives me compiler error "Class Appdelegate has no initializers" – Asim Khan Mar 10 '17 at 02:53
  • Is there any possibility for smoothly changing flashlight intensity over time? – Ishwar Hingu Mar 11 '20 at 09:29