Swift 3.0 打开手机手电筒

前端之家收集整理的这篇文章主要介绍了Swift 3.0 打开手机手电筒前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@IBAction func lightButtonClick(_ sender: UIButton) {
        let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
        if device == nil {
            sender.isEnabled = false
            return
        }
        if device?.torchMode == AVCaptureTorchMode.off{
            do {
                try device?.lockForConfiguration()
            } catch {
                return
            }
            device?.torchMode = .on
            device?.unlockForConfiguration()
            sender.isSelected = true
        }else {
            do {
                try device?.lockForConfiguration()
            } catch {
                return
            }
            device?.torchMode = .off
            device?.unlockForConfiguration()
            sender.isSelected = false
        }
    }

猜你在找的Swift相关文章