0

所以我有一个 UISwitch 用于我的叠加层,每次相机出现时都会出现。现在,当我打开按钮或向右滑动开关(开启模式)时,手电筒就会打开。但是当我把它切换到左边时,它并没有关闭。我究竟做错了什么?

- (void)mySwitchPressed {
    if (self.mySwitch.on) { 
        AVCaptureDevice *flashLight = [AVCaptureDevice
        defaultDeviceWithMediaType:AVMediaTypeVideo];
        if([flashLight isTorchAvailable] && [flashLight
            isTorchModeSupported:AVCaptureTorchModeOn]) {
            BOOL success = [flashLight lockForConfiguration:nil];
            if(success) {
                [flashLight setTorchMode:AVCaptureTorchModeOn];
                [flashLight unlockForConfiguration];
            }
        } else {
            AVCaptureDevice *flashLight = [AVCaptureDevice
            defaultDeviceWithMediaType:AVMediaTypeVideo];
            if([flashLight isTorchAvailable] && [flashLight 
                isTorchModeSupported:AVCaptureTorchModeOn]) {
                BOOL success = [flashLight lockForConfiguration:nil];
                if(success) {
                    [flashLight setTorchMode:AVCaptureTorchModeOff];
                    [flashLight unlockForConfiguration];
                }
            }
        }
    }
}
4

1 回答 1

1

重新格式化您的代码后,您的子句似乎else位于错误的位置。尝试在第一个块else结束后移动 to :if

- (void)mySwitchPressed {
    if (self.mySwitch.on) { 
        AVCaptureDevice *flashLight = [AVCaptureDevice
        defaultDeviceWithMediaType:AVMediaTypeVideo];
        if([flashLight isTorchAvailable] && [flashLight
            isTorchModeSupported:AVCaptureTorchModeOn]) {
            BOOL success = [flashLight lockForConfiguration:nil];
            if(success) {
                [flashLight setTorchMode:AVCaptureTorchModeOn];
                [flashLight unlockForConfiguration];
            }
        }
    } else {
        AVCaptureDevice *flashLight = [AVCaptureDevice
        defaultDeviceWithMediaType:AVMediaTypeVideo];
        if([flashLight isTorchAvailable] && [flashLight 
            isTorchModeSupported:AVCaptureTorchModeOn]) {
            BOOL success = [flashLight lockForConfiguration:nil];
            if(success) {
                [flashLight setTorchMode:AVCaptureTorchModeOff];
                [flashLight unlockForConfiguration];
            }
        }
    }
}
于 2012-07-31T00:16:47.890 回答