1

我有一个UISWitch默认位置设置为off

开关可选地on由用户设置,我如何将该开关从另一个按钮/动作转回关闭。

- (IBAction)switchToggled:(id)sender {

if (_toggle.on){


  AVCaptureDevice *backCamera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ( [backCamera isTorchAvailable] && [backCamera isTorchModeSupported:AVCaptureTorchModeOn]  )
{
    BOOL success = [backCamera lockForConfiguration:nil];
    if ( success )
    {
        [backCamera setTorchMode:AVCaptureTorchModeOn];
        [backCamera unlockForConfiguration];
      }
    }
}

else {

    AVCaptureDevice *backCamera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ( [backCamera isTorchAvailable] && [backCamera isTorchModeSupported:AVCaptureTorchModeOn]  )
    {
        BOOL success = [backCamera lockForConfiguration:nil];
        if ( success )
        {
            [backCamera setTorchMode:AVCaptureTorchModeOff];
            [backCamera unlockForConfiguration];
        }
    }

}

从 ui 按钮重置开关状态:

  - (IBAction)toggledOffSwitch:(id)sender {
     _toggle.on = NO; 
    }         

或者

  - (IBAction)toggledOffSwitch:(id)sender {
  [_toggle setOn:NO];
  [_toggle setEnabled: NO];
   _torch.enabled = NO;
   _torch.on = NO;

  }

似乎没有这样做

更新了更多代码

4

3 回答 3

1

你确定你- (IBAction)toggledOffSwitch:(id)sender在 Xcode 中绑定了你的动作吗?

于 2013-04-06T14:18:40.780 回答
0

你试过这个方法吗?

- (void)setOn:(BOOL)on animated:(BOOL)animated
于 2013-04-06T14:19:06.690 回答
0

好的,想通了这个

 - (IBAction)toggledOffSwitch:(id)sender {


AVCaptureDevice *backCamera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ( [backCamera isTorchAvailable] && [backCamera isTorchModeSupported:AVCaptureTorchModeOn]  )
{
    BOOL success = [backCamera lockForConfiguration:nil];
    if ( success )
    {
        [backCamera setTorchMode:AVCaptureTorchModeOff];
        [backCamera unlockForConfiguration];

        _torch.on = NO;
    }

    }

}
于 2013-04-06T15:23:51.777 回答