简体   繁体   English

iOS,WKInterfaceTimer启动计时器

[英]iOS , WKInterfaceTimer Start timer

I'm developing an app using WatchKit 我正在使用WatchKit开发一个应用程序

I want to create one WKInterfaceTimer , when I tap in one button the timer starts with 0 Second 我想创建一个WKInterfaceTimer ,当我点击一个按钮时,计时器以0秒开始

but when I run the application the timer automaticaly start and I'm not able to stop timer before or after 但是当我运行应用程序时,计时器自动启动,我无法在之前或之后停止计时器

here is the code: 这是代码:

 [self.mytimer setHidden:NO];
 [self.mytimer start];

In the storyboard I have set short "second minutes hour" all ticked and Enabled unticked 在故事板中,我设置了短暂的“第二分钟小时”,所有内容都勾选并启用了未启用

Do I need to use [self.mytimer setDate:]; 我需要使用[self.mytimer setDate:];

If yes please give me the exact for short string with showing label 0 secends 如果是,请给我一个确切的短字符串,显示标签0 secends

I'm not exactly sure what your asking but I placed a timer with a button below it in my storyboard for the watch. 我不确定你的问题,但我在我的故事板中放了一个带有按钮的计时器。 Then I made sure the timer was enabled with abbreviated format (Making sure second, minute, and hour was selected). 然后我确保使用缩写格式启用计时器(确保选择了秒,分钟和小时)。 Make sure the timer is enabled else it will not appear in your view and currently I could not find a way to enable the timer programmatically. 确保计时器已启用,否则它将不会出现在您的视图中,目前我找不到以编程方式启用计时器的方法。 Also make sure you set the date when you hit the start button or else the timer will start counting up without you knowing in the background. 还要确保在按下开始按钮时设置日期,否则计时器将在后台不知情的情况下开始计时。 In WKInterfaceTimer's class, the start method only updates the label and does not actually start any kind of timer. 在WKInterfaceTimer的类中,start方法仅更新标签,实际上并不启动任何类型的计时器。 The code below makes the timer count up from 0. 下面的代码使计时器从0开始计数。

#import "InterfaceController.h"


@interface InterfaceController()
@property (weak, nonatomic) IBOutlet WKInterfaceTimer *testTimer;
@end


@implementation InterfaceController

- (instancetype) initWithContext: (id) context
{
    self = [super initWithContext:context];
    if (self)
    {
        NSLog(@"Custom init called for InterfaceController!");
    }
    return self;
}

- (void) willActivate
{
    [self.testTimer stop];
    NSLog(@"Activated!");
}

- (IBAction) onStartButtonPressed
{
    [self.testTimer setDate:[NSDate dateWithTimeIntervalSinceNow:-1]];
    [self.testTimer start];
    NSLog(@"Start button pressed!");
}
@end

The reason for stopping the timer under willActivate is so that it will not start counting upwards as soon as the view is displayed. 在willActivate下停止计时器的原因是,一旦显示视图,它就不会开始向上计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM