闪烁手电筒,制作手电筒应用程序

时间:2013-10-12 03:55:14

标签: ios

我正在制作手电筒应用程序。

它的功能是1.on / off torch,2.blink touch(带开关和滑块)

这是我的所有代码

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 个答案:

答案 0 :(得分:1)

您应该设置NSTimer。

从拥有NSTimer的属性开始,这样您就可以使其无效以使其稍后停止。

@property (nonatomic, strong) NSTimer *blinkTimer.

以下是我在代码中创建计时器的示例:

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

这将每0.3秒调用一次timerUpdate。

在timerUpdate中,您可以打开/关闭手电筒。

如果您希望停止,只需执行[self.blinkTimer invalidate];