如何在我的视频应用程序中添加“计数”计时器 - iOS

时间:2014-11-25 03:20:42

标签: ios video nstimer

我的应用程序允许用户录制视频。它不使用UIImagePickerController,也不希望它。目前,该应用程序显示红色" REC"应用程序捕获视频时,相机视图控制器中的按钮。我希望在录制开始时添加一个倒计时器...指示应用程序录制视频的时间。

以下是我们如何获得" REC"按钮已配置;

- (无效)btn_start_recording_clicked {

if (!WeAreRecording)
{
    custom_log(@"Start Recording");
    [self performSelector:@selector(record_animation) withObject:nil    afterDelay:2.0];
    img_stop_video_rec.image = [UIImage imageNamed:@"start_recording.png"];

我找不到任何相关内容。非常感谢。

1 个答案:

答案 0 :(得分:0)

更新答案

您需要使计时器无效以停止计数:

-(void)stopTimer
{
    [self.timer invalidate];
    self.timer = nil;

    [UIView animateWithDuration:1.0 animations:^{
        self.lblTimer.alpha = 0;
    }];
}

这是您创建UITapGesture的新viewDidLoad()方法。我不知道您的应用程序是如何工作的,但我会说您应该有一个停止记录按钮并将其连接到上述方法。我的新viewDidLoad()方法看起来像这样:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(stopTimer)];
    tapGesture.numberOfTapsRequired = 2;

    self.lblTimer = [[UILabel alloc] init];
    self.lblTimer.translatesAutoresizingMaskIntoConstraints = NO;
    self.lblTimer.textAlignment = NSTextAlignmentCenter;
    self.lblTimer.font = [UIFont systemFontOfSize:32];

    self.lblTimer.userInteractionEnabled = YES;
    [self.lblTimer addGestureRecognizer:tapGesture];




    // set the starting total seconds
    self.totalSeconds = 0;

    [self setTimerTextWithTotalSeconds:self.totalSeconds];

    [self.view addSubview:self.lblTimer];

    // adding constraints
    id views = @{@"lblTimer": self.lblTimer};

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[lblTimer]|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[lblTimer]|" options:0 metrics:nil views:views]];

    // start the timer
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
}

你有没有试过这样的东西:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (nonatomic, strong) UILabel *lblTimer;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) int totalSeconds;


@end

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.lblTimer = [[UILabel alloc] init];
    self.lblTimer.translatesAutoresizingMaskIntoConstraints = NO;
    self.lblTimer.textAlignment = NSTextAlignmentCenter;
    self.lblTimer.font = [UIFont systemFontOfSize:32];

    // set the starting total seconds
    self.totalSeconds = 0;

    [self setTimerTextWithTotalSeconds:self.totalSeconds];

    [self.view addSubview:self.lblTimer];

    // adding constraints
    id views = @{@"lblTimer": self.lblTimer};

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[lblTimer]|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[lblTimer]|" options:0 metrics:nil views:views]];

    // start the timer
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
}

-(void)setTimerTextWithTotalSeconds:(int)totalSeconds
{
    int remainder = 0;

    int hours = totalSeconds / 3600;
    remainder = totalSeconds % 3600;

    int minutes = remainder / 60;
    remainder = remainder % 60;

    int seconds = remainder;

    NSString *strTotalTime = [[NSString alloc] initWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];

    self.lblTimer.text = strTotalTime;
}

// ----------------------------------------
// Adds 1 second to total seconds
// ----------------------------------------
-(void)updateTimer:(NSTimer *)timer
{
    self.totalSeconds += 1;

    [self setTimerTextWithTotalSeconds:self.totalSeconds];
}

你应该看到这样的事情:

screenshot