2014-05-20 217 views
-2

所以我試圖創建一個LED閃光燈,我已經設法爲燈光開/關開關。這裏是我的代碼:實施閃光燈功能

@implementation ViewController 
- (void) setTorchOn:(BOOL)isOn 
{ 
AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
[device lockForConfiguration:nil]; 
[device setTorchMode:isOn ? AVCaptureTorchModeOn : AVCaptureTorchModeOff]; 
[device unlockForConfiguration]; 

} 

-(IBAction)changedSate:(id)sender { 
UISwitch *switchValue = (UISwitch*)sender; 

[self setTorchOn:[switchValue isOn]]; 

我想知道是否有人可以幫助我與這部分。

+1

如果你問了一個具體的問題,我相信 – Pseudonym

+2

會有所幫助。你有什麼問題?你有什麼嘗試? –

回答

0

只要做一個循環,不斷打開和關閉火炬。循環的類型取決於你希望如何實現。

0

我認爲你應該使用NSTimer類來反覆切換火炬。還有其他的方法,但只是做不是循環與睡眠()調用。

// 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. 

可能是你在找什麼。

更新:我知道這是不是你原來問過,但我知道這是通常最好通過例子來學習,所以這裏是一個使用這種(未經測試)的完整的例子:

@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]; 
} 
} 

這將每當你打開開關就開始閃爍,一旦關閉開關就停止閃爍。

+0

好的,UIbutton的代碼應該是什麼? – user3658255

+0

你能告訴我完整的代碼示例嗎? – user3658255

+0

專用於@ 9000 – user3658255