-3

懐中電灯アプリを作っています。

その機能は、1.トーチのオン/オフ、2.点滅タッチ(スイッチとスライダー付き)です。

これが私のすべてのコードです

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

トーチ(懐中電灯)を点滅させるにはどうすればよいですか?

4

1 に答える 1

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

于 2013-10-12T04:29:08.587 に答える