Ios 如何使用一个按钮打开/关闭手电筒?
我可以用一个按钮打开手电筒,用另一个按钮关闭手电筒。但我只想用一个按钮。然而,我没有一个框架允许我使用bool-isSelected方法。所以我对如何在一个按钮中将两个函数合并在一起一无所知 以下是有效的代码:Ios 如何使用一个按钮打开/关闭手电筒?,ios,Ios,我可以用一个按钮打开手电筒,用另一个按钮关闭手电筒。但我只想用一个按钮。然而,我没有一个框架允许我使用bool-isSelected方法。所以我对如何在一个按钮中将两个函数合并在一起一无所知 以下是有效的代码: -(void)onButtonPressed { AVCaptureDevice *flashLight = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if([flashLight isTorchA
-(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];
}
}
}
我对它的做法并不挑剔。只要第一次轻触时手电筒打开,第二次轻触时手电筒关闭,我就不在乎这个方法了
然而,我使用的是编程制作的Barbuttonims,所以请不要给我IBAction方法。如果建议的方法尽可能简单,我也会很感激。我认为我现在使用手电筒的方式过于复杂。在这里,我的想法如下:
.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()
,而不是您一直在调用的内容。这应该是您想要的,非常欢迎您调整它的一部分(您可能需要),但这是代码形式的一般想法。希望这对你有用 我刚刚在我的应用程序上实现了这个功能。回答您的问题,下面是如何在一个方法中合并这两个函数
- (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)上的手电筒。 解决方案:
斯威夫特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")
}
}
}基于Chuy47的答案的Swift 5解决方案:
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)")
}
}
}
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)
}
我想到了一个UI开关,它能满足您的要求吗?那很好,但我如何在工具栏覆盖上实现它呢?请记住,我必须以编程的方式进行。啊,我有一个更好的主意。如果您在类中创建了一个计数器,第一次按下按钮时,它会将计数器增加到1(开),第二次它会将计数器重置为0(关),那么您可以执行if-else语句并执行闪光灯开/关代码。这样,您可以删除您的其他按钮。让我知道这是否对你有效,我可以添加它作为你接受和解决这个问题的答案。理论上这听起来很好,但我将如何确切地创建计数器。我明白你的意思,但是你能用我放在这里演示的代码吗?当然,给我一点时间写下来作为回答,我会做一些假设,所以如果你的情况不准确,请告诉我。非常感谢!我一直在努力,但过了一会儿就放弃了。如果你能回答我的新问题,我将不胜感激。自从你给了我一个开关的想法,我就决定用它来代替巴布托。“这大大提高了应用程序的质量”。@Andrewladdedis嗯,我想知道它的哪一部分不起作用,上面的代码应该做你想做的事情。在任何情况下,请务必通过删除或接受此答案来解决此问题。我也回答了你的新问题,我希望这对你更有效!
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)")
}
}
}
@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)
}