【问题标题】:How do I add a "count up" timer to my video app - iOS如何在我的视频应用程序中添加“计数”计时器 - iOS
【发布时间】:2014-11-25 03:20:42
【问题描述】:

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

这里是我们如何配置“REC”按钮的一些示例代码;

-(void)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"];

我在这方面找不到任何东西。非常感谢帮助。

【问题讨论】:

  • 我很惊讶你设法制作了一个可以进行视频录制但无法计时的应用程序:D 我在下面添加了一个演示应用程序,看看你是不是这样之后。

标签: ios video nstimer


【解决方案1】:

更新答案

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

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

您应该会看到如下内容:

【讨论】:

  • 你好张;这很好用,谢谢!有一个问题。它不会停止。在我的应用程序中,用户双击屏幕以停止录制。当用户双击时,我需要计时器停止并消失。有什么想法吗?
  • 由于我添加了计时器,应用程序现在每 0.5 秒保存一次视频。我放了 10 秒钟,当我退出画廊时,有 20 个新视频。有什么想法吗?
  • 我不知道你是如何录制视频的抱歉。创建一个新的 Stackoverflow 问题并将代码粘贴到执行保存视频操作的位置。其他人或许可以帮助您。
  • 你知道如何停止和重置计时器吗?
  • @user3294722 你认真写过视频应用吗?我开始怀疑哈哈。我已经给了你上面关于如何停止计时器的代码。要重置计时器,请清除 totalSeconds 属性,然后更新文本标签。
猜你喜欢
  • 1970-01-01
  • 2021-08-10
  • 1970-01-01
  • 2014-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多