2013-10-12 101 views
-3

我正在製作手電筒應用程序。閃爍手電筒,使手電筒應用程序

其職能是1.on /打火,2.blink觸摸(帶開關和滑塊)

這裏是我的代碼

ViewController.m 

// 
// ViewController.m 
// Just Flashlight 
// 
// Created by CenoX on 2013. 10. 9.. 
// Copyright (c) 2013년 SHIFTstudios. All rights reserved. 
// 

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 



- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.blinksliderlabel.text = @"150ms"; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)flash:(UIButton *)sender { 

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 

    if ([device hasTorch] == NO) 
    { 
     [device setTorchMode:AVCaptureTorchModeOn]; 
     [_flashton setHighlighted:YES]; 
    } else { 
     [device setTorchMode:AVCaptureTorchModeOff]; 
     [_flashton setHighlighted:NO]; 
    } 
} 

- (IBAction)blinkspeed:(UISlider *)sender { 
    int progress = lroundf(sender.value); 
    self.blinksliderlabel.text = [NSString stringWithFormat:@"%dms", progress]; 
} 
@end 

ViewController.h 

// 
// ViewController.h 
// Just Flashlight 
// 
// Created by CenoX on 2013. 10. 9.. 
// Copyright (c) 2013년 SHIFTstudios. All rights reserved. 
// 

#import <UIKit/UIKit.h> 
#import <AVFoundation/AVFoundation.h> 

@interface ViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UIImageView *flashton; 
- (IBAction)flash:(UIButton *)sender; 
@property (weak, nonatomic) IBOutlet UILabel *blinksliderlabel; 
- (IBAction)blinkspeed:(UISlider *)sender; 
@property (weak, nonatomic) IBOutlet UISwitch *blinkswitch; 


@end 

如何閃爍的火炬(手電筒)。

回答

1

你應該設置一個NSTimer。

從擁有NSTimer的屬性開始,所以您可以使其失效以便稍後停止。

@property (nonatomic, strong) NSTimer *blinkTimer. 

下面是我如何在我的代碼創建一個定時器樣本:

self.blinkTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES]; 

這將調用timerUpdate每0.3秒。

在timerUpdate中,您可以打開/關閉手電筒。

當你想要它停止時,只要做[self.blinkTimer invalidate];