【问题标题】:How to turn flashlight on/off using one button?如何使用一键打开/关闭手电筒?
【发布时间】:2020-07-08 19:10:30
【问题描述】:

我可以用一个按钮打开手电筒,用另一个按钮将其关闭。但我只想用一个按钮来完成。但是,我没有允许我使用 bool isSelected 方法的框架。所以我对如何将两个功能合并到一个按钮中一无所知。

这是有效的代码:

-(void)onButtonPressed 
{

AVCaptureDevice *flashLight = [AVCaptureDevice 
defaultDeviceWithMediaType:AVMediaTypeVideo];
if([flashLight isTorchAvailable] && [flashLight
isTorchModeSupported:AVCaptureTorchModeOn])
{
    BOOL success = [flashLight lockForConfiguration:nil];
    if(success){
        [flashLight setTorchMode:AVCaptureTorchModeOn];
        [flashLight unlockForConfiguration];
    }
}

}

我用它来关掉手电筒。

-(void)offButtonPressed {

AVCaptureDevice *flashLight = [AVCaptureDevice
defaultDeviceWithMediaType:AVMediaTypeVideo];
if([flashLight isTorchAvailable] && [flashLight
isTorchModeSupported:AVCaptureTorchModeOn])
{
    BOOL success = [flashLight lockForConfiguration:nil];
    if(success){
        [flashLight setTorchMode:AVCaptureTorchModeOff];
        [flashLight unlockForConfiguration];
    }
}


}

我并不特别关注它的完成方式。只要手电筒在第一次轻敲时打开并在第二次轻按时关闭,我就不会关心这种方法。

但是,我正在使用以编程方式制作的 barbuttonitems,所以请不要给我 IBAction 方法。如果建议的方法尽可能简单,我也很感激,我认为我现在使用手电筒的方式过于复杂。

【问题讨论】:

  • 我想到了一个 UISwitch,这能满足你的要求吗?
  • 那很好,但我如何在工具栏覆盖上制作它?请记住,我必须以编程方式进行。
  • 啊,我有个更好的主意。如果您在课堂上创建了一个计数器怎么办/关闭代码。这样您就可以删除其他按钮。让我知道这是否适合您,我可以将其添加为您接受并解决此问题的答案。
  • 理论上听起来很棒,但我将如何创建计数器。我明白你在说什么,但你能用我放在这里的代码来演示吗?
  • 请给我一秒钟的时间写下来作为答案,我会做一些假设,所以如果您的情况有什么不准确的地方,请告诉我。

标签: ios


【解决方案1】:

我刚刚在我的应用上实现了这个功能。回答您的问题,这里是如何在一种方法中合并这两个函数。

- (void) flashlight
{
    AVCaptureDevice *flashLight = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([flashLight isTorchAvailable] && [flashLight isTorchModeSupported:AVCaptureTorchModeOn])
    {
        BOOL success = [flashLight lockForConfiguration:nil];
        if (success) 
        {
            if ([flashLight isTorchActive]) {
                [flashLight setTorchMode:AVCaptureTorchModeOff];
            } else {
                [flashLight setTorchMode:AVCaptureTorchModeOn];
            }
            [flashLight unlockForConfiguration];
        }
    }
}

【讨论】:

    【解决方案2】:

    对于 Swift 3

    @IBAction func toggleFlash() {
        if let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo), device.hasTorch {
        do {
            try device.lockForConfiguration()
            let torchOn = !device.isTorchActive
            try device.setTorchModeOnWithLevel(1.0)
            device.torchMode = torchOn ? .on : .off
            device.unlockForConfiguration()
        } catch {
            print("error")
        }
    }
    

    }

    【讨论】:

      【解决方案3】:

      问题陈述:


      使用单个按钮打开和关闭移动设备上的手电筒(支持 iOS)。

      解决方案

      1. 在视图控制器上创建一个按钮。
      2. 设置按钮动作并添加目标 作为自己。

      注意: 执行前在interface builder中设置tag为101,这会初始化tag值为101。

      以下是应该在 ViewController.m 文件中以 action 方法编写的代码


      - (IBAction)flashLightButtonTapped:(id)sender
      {
      
          UIButton *button = sender;
          AVCaptureDevice *flashLight = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
      
          if (button.tag==101)
          {
      
              if ([flashLight isTorchAvailable] && [flashLight isTorchModeSupported:AVCaptureTorchModeOn])
              {
                  BOOL success = [flashLight lockForConfiguration:nil];
                  if (success)
                  {
                      if ([flashLight isTorchActive])
                      {
                          [flashLight setTorchMode:AVCaptureTorchModeOff];
                      }
                      else
                      {
                          [flashLight setTorchMode:AVCaptureTorchModeOn];
                      }
                      [flashLight unlockForConfiguration];
                  }
              }
              button.tag=102;
          }
          else if (button.tag==102)
          {
              if ([flashLight isTorchAvailable] && [flashLight isTorchModeSupported:AVCaptureTorchModeOn])
              {
                  BOOL success = [flashLight lockForConfiguration:nil];
                  if (success)
                  {
                      if ([flashLight isTorchActive])
                      {
                          [flashLight setTorchMode:AVCaptureTorchModeOn];
                      }
                      else
                      {
                          [flashLight setTorchMode:AVCaptureTorchModeOff];
      
                      }
                      [flashLight unlockForConfiguration];
                  }
              }
              button.tag=101;
          }
      
      }
      

      【讨论】:

        【解决方案4】:

        这里要关闭cmets,这就是我的想法:

        .h
        
        @property (nonatomic) int flashlightState;
        
        .m
        
        -(void) viewDidLoad:
        {
            //Any previous code that you may have
            flashlightState = 0;
        }
        -(void) flashlightStateControl:
        {
        
            if (flashlightState == 1)
            {
                offButtonPress();
                flashlightState = 0;
            }
            else
            {
                onButtonPress();
                flashlightState = 1;
            }
        }
        

        这个想法是,你现在调用flashlightStateControl(),而不是你一直在调用的东西。那应该做你想做的,非常欢迎你调整它的一部分(你可能需要),但这是代码形式的一般想法。希望对你有用!

        【讨论】:

        • 非常感谢!我一直调整到没有结束,但一段时间后放弃了。如果您回答我的新问题,我将不胜感激。自从你给了我一个开关的想法,我决定使用它而不是 barbuttonitem。它大大提高了应用的质量。
        • @AndrewLaeddis 嗯,我想知道它的哪一部分不起作用,上面的代码应该做你想做的事。无论如何,请务必通过删除或接受此答案来解决此问题。我也回答了你的新问题,希望对你更好!
        【解决方案5】:
        - (IBAction)flash:(UIButton *)sender;
        {
            Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
            if (captureDeviceClass != nil) {
                AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
                if ([device hasTorch] && [device hasFlash]){
        
                    [device lockForConfiguration:nil];
                    if (device.torchMode == AVCaptureTorchModeOff) {
                        [device setTorchMode:AVCaptureTorchModeOn];
                        [device setFlashMode:AVCaptureFlashModeOn];
                        //TorchIsOn = YES;
        
                        [self.flashton setHighlighted:YES];
                    } else {
                        [device setTorchMode:AVCaptureTorchModeOff];
                        [device setFlashMode:AVCaptureFlashModeOff];
                        //TorchIsOn = NO;
        
                        [self.flashton setHighlighted:NO];
                    }
                    [device unlockForConfiguration];
                }
            }
        }
        

        【讨论】:

          【解决方案6】:

          Swift 5 的解决方案基于 Chuy47 的回答:

          import Foundation
          import AVFoundation
          
          extension AVCaptureDevice {
          
              /// toggles the device's flashlight, if possible
              static func toggleFlashlight() {
                  guard let device = AVCaptureDevice.default(for: AVMediaType.video), device.hasTorch else { return }
                  do {
                      try device.lockForConfiguration()
                      let torchOn = !device.isTorchActive
                      try device.setTorchModeOn(level: 1.0)
                      device.torchMode = torchOn ? .on : .off
                      device.unlockForConfiguration()
                  } catch {
                      print("Error toggling Flashlight: \(error)")
                  }
              }
          }
          

          【讨论】:

            【解决方案7】:

            Swift 5 版本

            @IBAction func toggleTorch() {
                guard let device = AVCaptureDevice.default(for: .video), device.hasTorch else {
                    showTorchNotSupported()
                    return
                }
                
                do {
                    try device.lockForConfiguration()
                    let torchOn = !device.isTorchActive
                    try device.setTorchModeOn(level: 1.0)
                    device.torchMode = torchOn ? .on : .off
                    device.unlockForConfiguration()
                } catch {
                    showTorchNotSupported()
                }
            }
            
            private func showTorchNotSupported() {
                let alertController = UIAlertController(title: "Flashlight is not supported", message: nil, preferredStyle: .alert)
                alertController.addAction(UIAlertAction(title: "Understand", style: .default, handler: nil))
                present(alertController, animated: true)
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2019-10-05
              • 2020-06-04
              • 1970-01-01
              • 2015-01-28
              • 2019-03-18
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多