我可以用一个按钮打开手电筒,然后用另一个按钮关闭。但我只想用一个按钮来做。但是,我没有允许我使用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方法。如果建议的方法尽可能简单,我也很感激,我认为我现在使用手电筒的方式过于复杂。
答案 0 :(得分:30)
我刚刚在我的应用上实现了此功能。回答你的问题,这里是如何在一种方法中合并两个函数。
- (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];
}
}
}
答案 1 :(得分:5)
对于swift 3
time.sleep
}
答案 2 :(得分:1)
问题陈述:
解决方案:
注意:在执行前在接口构建器中将标记设置为101,这将启动标记值为101.
以下是应在 ViewController.m 文件中以操作方法编写的代码
- (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;
}
}
答案 3 :(得分:0)
在这里发表评论,这就是我的想法:
.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()
而不是你一直在打电话。这应该做你想要的,你非常欢迎你调整它的一部分(你可能需要),但这是代码形式的一般想法。希望能为你效劳!
答案 4 :(得分:0)
- (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];
}
}
}
答案 5 :(得分:0)
Swift 5解决方案基于Chuy47的答案:
left:
答案 6 :(得分:0)
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)
}