How to set two buttons to turn on/off vibration on iOS ?
Pergunta
How to set two buttons to turn on/off vibration on iOS ?
This is what I was trying to do:
@property (nonatomic) BOOL vibeIsOn;
- (IBAction)startVibrating:(id)sender {
dispatch_queue_t vibeQueue = dispatch_queue_create("vibe", NULL);
dispatch_sync(vibeQueue, ^{
for (;!self.vibeIsOn;)
{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
});
dispatch_release(vibeQueue);}
- (IBAction)stopVibrating:(id)sender {
self.vibeIsOn = YES;
AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);}
Unfortunately, when I press the "staring vibrating" button, it just can not jump out of the for loop, but I did put the for loop in a thread, right?
Help!!! Is anything wrong with this code?
Solução
You dispatch the queue synchronously, so the calling thread waits until the execution finishes (which never happens because you for loop never stops). Use dispatch_async
instead.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow