如何使用一个button打开/closures手电筒?

我可以用一个button打开手电筒,并用另一个buttonclosures手电筒。 但是我只想用一个button来做。 但是,我没有一个框架,它允许我使用BOOL isSelected方法。 所以我对如何在一个button中将两个函数合并在一起非常无知。

以下是可用的代码:

-(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]; } } } 

我并不特别关心它的完成方式。 只要手电筒开着第一个水龙头,closures第二个,我不能不在乎这个方法。

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

我刚刚在我的应用程序上实现了这个function。 回答你的问题,这里是如何在一个方法中合并两个函数。

 - (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]; } } } 

快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") } } 

}

关于这里的评论,这是我在想什么:

 .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() 。 这应该做你想要的,你是欢迎调整它的一部分(你可能需要),但这是代码forms的一般想法。 希望能为你解决!

 - (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]; } } } 

问题陈述 :


打开和closures手机上的手电筒(支持iOS)使用一个button。

解答

  1. 在View Controller上创build一个button。
  2. 设置button操作并添加目标为自己。

注意:在执行之前,在界面生成器中将标签设置为101,这将启动标签值为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; } }