首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让FlashLight闪烁?

如何让FlashLight闪烁?
EN

Stack Overflow用户
提问于 2018-10-02 19:41:12
回答 1查看 221关注 0票数 0

我有以下代码来打开和关闭手电筒:

代码语言:javascript
复制
func toggleTorch(on: Bool) {
    guard let device = AVCaptureDevice.default(for: AVMediaType.video)
        else {return}

    if device.hasTorch {
        do {
            try device.lockForConfiguration()

            if on == true {
                device.torchMode = .on
            } else {
                device.torchMode = .off
            }

            device.unlockForConfiguration()
        } catch {
            print("Torch could not be used")
        }
    } else {
        print("Torch is not available")
    }
}

我想让它一直闪烁直到我按下按钮。我怎么才能做到呢?

EN

回答 1

Stack Overflow用户

发布于 2018-10-02 20:01:01

你可以试试

代码语言:javascript
复制
var btnNotPressed = true

func blink() {

 if device.hasTorch {
    do {
        try device.lockForConfiguration()

        if on == true {
            device.torchMode = .on
        } else {
            device.torchMode = .off
        }
        // toggle on
        on = !on
        device.unlockForConfiguration()
       // delay until flash shows/hides you can make it 0.5 if you it more fast
       DispatchQueue.main.asyncAfter(deadline:.now() + 1 ) {
         if btnNotPressed {
           self.blink()
         }
       }

    } catch {
        print("Torch could not be used")
    }
  } else {
    print("Torch is not available")
  }
}

然后在按下按钮时设置btnNotPressed

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52607642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档