gpt4 book ai didi

ios - 如何使用一个按钮打开/关闭手电筒?

转载 作者:技术小花猫 更新时间:2023-10-29 10:41:34 24 4
gpt4 key购买 nike

我可以用一个按钮打开我的手电筒,然后用另一个按钮关闭它。但我只想用一个按钮来完成。但是,我没有允许我使用 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 方法。如果建议的方法尽可能简单,我也将不胜感激,我认为我现在使用手电筒的方式过于复杂。

最佳答案

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

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

关于ios - 如何使用一个按钮打开/关闭手电筒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11726543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com