Question about dispatch_timer function
Question about dispatch_timer function
- Subject: Question about dispatch_timer function
- From: Kévin Vavelin <email@hidden>
- Date: Thu, 26 Apr 2012 03:07:05 +0200
Hi there,
I've a question about the dispatch_timer function. I try to make a strobe flash on my iPhone app and for that i used an NSTimer but a friend of mine say that it's better to use dispatch function. So i was looking and try to implement something. I explain my code before post anything :
I have a UISlider in another view (settings.strobeValue) and get his value for settings the timer. And i called changeStrobeState in another function who wll be called by dispatch function. But when i hit the button on my view and send my dispatch code, no work at all... So there is the code :
-(void)changeStrobeState
{
if (NO == [[NSUserDefaults standardUserDefaults] boolForKey:@"buttonStrobeState"])
{
[deviceCapture lockForConfiguration:nil];
[deviceCapture setTorchMode:AVCaptureTorchModeOff];
[deviceCapture unlockForConfiguration];
}
else
{
[deviceCapture lockForConfiguration:nil];
[deviceCapture setTorchMode:AVCaptureTorchModeOn];
[deviceCapture unlockForConfiguration];
}
BOOL state = [[NSUserDefaults standardUserDefaults] boolForKey:@"buttonStrobeState"];
[[NSUserDefaults standardUserDefaults] setBool:!state forKey:@"buttonStrobeState"];
}
- (dispatch_source_t)setupStrobeTimer {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_strobeTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
if (_strobeTimer) {
dispatch_source_set_timer(_strobeTimer, dispatch_walltime(DISPATCH_TIME_NOW, settings.strobeValue * NSEC_PER_SEC),
settings.strobeValue * NSEC_PER_SEC, 6ull * NSEC_PER_SEC);
__block ViewController *blockSelf = self;
dispatch_source_set_event_handler(_strobeTimer, ^{
[blockSelf changeStrobeState];
});
}
});
return _strobeTimer;
}
Can you help me ?
Thanks for reading and sorry 'cause my english is not perfect and it's actually 3 AM here :)
Cheers,
Vavelin Kevin
Twitter | Blog | Viadeo
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden