Tag: nstimer

不能无效,停止倒计时NSTimer – Objective C

问题是停止NSTimer,出于某种原因[计时器无效]只是不工作… 也许我的眼睛里充满了肥皂,但却无法理解计时器不是停在0的原因,而是反向计数-1,-2,-3等等…((( 我使用纪元号码作为目的地date。 还有一件事 – 我的button“IBAction停止”与[计时器无效]工作得很好 – 当我推它在模拟器计时器停止… @implementation ViewController – (void)viewDidLoad { [super viewDidLoad]; Timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES]; } – (IBAction) start { Timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES]; } – (IBAction) stop { [Timer invalidate]; Timer = nil; } -(void)updateLabel { NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; […]

制作倒数计时器并在后台运行

我想添加一个静音button到我的应用程序,当用户点击它,他select一个持续时间的静音,而静音启用用户不会从我的应用程序通知。 我以为NSTimer是一个不错的select,但是我怎样才能做出一个计时器呢? 我怎么能设置timer将在后台运行? 注意:我在后台使用位置监控。

如何使多次启​​动的NSTimer失效

我在Swift做了一个实践项目来学习NSTimer如何工作。 有一个button启动计时器和一个button来使其失效。 当我点击每个button一次,它工作正常。 但是,当我多次点击启动定时器button时,我不再能使其无效。 这是我的代码: class ViewController: UIViewController { var counter = 0 var timer = NSTimer() @IBOutlet weak var label: UILabel! @IBAction func startTimerButtonTapped(sender: UIButton) { timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "update", userInfo: nil, repeats: true) } @IBAction func cancelTimerButtonTapped(sender: UIButton) { timer.invalidate() } func update() { ++counter label.text = "\(counter)" } } […]

如何在UITableViewCell的标签中显示NSTimer的时间值

…继续问题如何在UITableViewCell中设置UIButton的Action 我有NSTimer在单身人士工作。 只有一个计时器,但它包含一个包含时间值的几个项目的词典。 每分钟计时器从字典中所有项目的时间值递减1。 字典的结构是: 键:“1”,值:与对象的数组NSString,NSInteger(时间值) TimerCell是一个tableViewControllers的单元格。 UITableViewController出现在UIPopoverController中。 所以我想在我的cell.label中显示字典的项目的时间值… 编辑:find方式 我已经创build了新的NSTimer在我的UITableViewController ,它始于viewDidLoad和每1秒采取新的价值单身的计时器,并显示它在我的cell.label … 如果这种方式不正确或有内存泄漏,请纠正我))

如何重置目标C中的计时器?

我在这个应用程序创build一个计数器,它开始时,我点击一个button,并停止当我点击相同的button。 问题是,当我再次启动时,显示计数的标签不会重置,从而停止计数。 所以任何人都可以帮助我? – (void)timerTick:(NSTimer *)timer { _lblCounter.text = @"0:00:00"; double seconds = 0.0; double minutes = 0.0; double hours = 0.0; ticks += 1.0; seconds = fmod(ticks, 60.0); minutes = fmod(trunc(ticks / 60.0), 60.0); hours = trunc(ticks / 3600.0); _lblCounter.text = [NSString stringWithFormat:@"%2.0f:%02.0f:%02.0f", hours, minutes, seconds]; } – (IBAction)btnProcessa:(UIButton *)sender { if ([sender.titleLabel.text isEqualToString:@"Iniciar"]) […]

如何在应用程序处于后台时每隔n分钟调用一次方法?

大家好,我需要调用一个方法,当应用程序在后台。 这是现在我正试图实现这一点,但计时器从来没有被调用。 – (void)applicationDidEnterBackground:(UIApplication *)application { UIApplication* app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; // Start the long-running task and return immediately. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Do the work associated with the task. self.timer = nil; [self initTimer]; }); } – (void)initTimer { DebugLog(@"timer INIT!!!!!"); if (self.timer == […]

在使用UIBackgroundModes = location的同时定期唤醒应用程序

我正在开发一个导航应用程序,它使用UIBackgroundModes=location设置,并通过didUpdateToLocation接收CLLocationManager更新。 这工作正常。 问题是,位置更新之间的间隔很难预测,我需要确保应用程序每隔几秒钟调用一次,即使位置没有明显变化,也可以执行一些其他(微小的)工作量。 我可以这样做吗? 我可以这样做吗? 我该怎么做? 我发现了一篇博客文章 ,但我不确定这是否是真正的方法。

位置服务,如何在一段时间后(30秒)更新纬度,经度?

我从位置服务获取纬度,经度,高度,准确度,速度。 我想每30秒更新一次这些信息。 为此我使用了NSTimer: [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(locationUpdate:) userInfo:nil repeats:YES]; 我把它放在viewDidLoad方法中。 它第一次显示输出,但第二次,当计时器调用它时,显示这个错误: 2011-08-02 13:17:00.141 CoreLocationAssign[1120:207] This is location <__NSCFTimer: 0x4b200a0> 2011-08-02 13:17:00.142 CoreLocationAssign[1120:207] -[__NSCFTimer coordinate]: unrecognized selector sent to instance 0x4b200a0 2011-08-02 13:17:00.144 CoreLocationAssign[1120:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFTimer coordinate]: unrecognized selector sent to instance 0x4b200a0' *** Call stack at first […]

如何在iOS设备中一秒钟抓取30个截图?

在我的iOS应用程序中,我使用NSTime来调用一个截取设备屏幕截图的方法。 我每秒钟截取30个截图。 在模拟器上工作正常(因为Mac的处理器比iPad强大得多)。 但是在实际设备中占用的屏幕截图数量较less。 我怎样才能做到这一点? 任何可靠的替代计时器? 请帮忙

暂停和重新创build计时器与通知问题Swift

所以我有两个计时器,一个提高分数,一个产生敌人。 我使用通知来使计时器无效,然后我使用另一个来重新创build计时器。 当我退出,然后打开应用程序,有两组敌人彼此顶部产卵。 我认为timerRecreate = true,GameScene中的常规定时器也被调用。 GameViewController.swift文件: class GameViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseTimers:"), name:UIApplicationWillResignActiveNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("startTimers:"), name:UIApplicationDidBecomeActiveNotification, object: nil) } func pauseTimers(notification : NSNotification) { println("Observer method called") timer.invalidate() scoretimer.invalidate() } func startTimers(notification : NSNotification) { println("Observer method called") timerRecreate = true } GameScene.swift中的定时器代码 override func […]