Je pense que vous devriez utiliser la classe NSTimer pour basculer à plusieurs reprises la torche. Il y a d'autres façons, mais faites simplement et non boucle avec un appel sleep().
// Have an NSTimer* timer and BOOL torchOn and volatile BOOL stopStrobe property in your class...
- (void) startFlashing{
self.timer = [[NSTimer alloc] initWithFireDate:[NSDate timeInvervalSinceNow: 0] interval:0.1 target:self selector:@selector(toggleTorch) userInfo:nil repeats:YES];
}
- (void) toggleTorch{
if (stopStrobe){
[self.timer invalidate];
}
torchOn = !torchOn
[self setTorchOn:torchOn];
}
// Set stopStrobe to YES elsewhere in your program when you want it to stop.
est probablement ce que vous cherchez.
MISE À JOUR:
@interface ViewController()
@property(nonatomic) BOOL torchOn;
@property(atomic) BOOL stopStrobe;
@end
@implementation ViewController
- (id) init{
self = [super init];
if (self){
self.torchOn = NO;
self.stopStrobe = NO;
}
}
- (void) setTorchOn:(BOOL)isOn
{
AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
[device setTorchMode:isOn ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
[device unlockForConfiguration];
}
- (void) toggleTorch{
if (stopStrobe){
[self.timer invalidate];
}
self.torchOn = !self.torchOn
[self setTorchOn:self.torchOn];
}
- (void) startFlashing{
self.timer = [[NSTimer alloc] initWithFireDate:[NSDate timeInvervalSinceNow: 0] interval:0.1 target:self selector:@selector(toggleTorch) userInfo:nil repeats:YES];
}
-(IBAction)changedSate:(id)sender {
UISwitch *switchValue = (UISwitch*)sender;
if ([switchValue isOn]{
self.stopStrobe = NO;
[self startFlashing];
}
else{
[self.stopStrobe = YES];
}
}
Cette volonté: Je sais que ce n'est pas ce que vous avez initialement demandé, mais je sais qu'il est souvent préférable d'apprendre par exemple, alors voici l'exemple complet d'utilisation de ce (non testé) commencez à clignoter chaque fois que vous mettez l'interrupteur en marche et arrêtez-le lorsque vous éteignez l'interrupteur.
Aiderait si vous posiez une question spécifique je crois – Pseudonym
D'accord. Quels problèmes avez-vous? Qu'avez-vous essayé? –