Tag: nstimer

如何立即触发NSTimer?

我需要调用一个方法,当我的应用程序启动,然后调用每5秒相同的方法。 我的代码很简单: // Call the method right away [self updatestuff:nil] // Set up a timer so the method is called every 5 seconds timer = [NSTimer scheduledTimerWithTimeInterval: 5 target: self selector: @selector(updatestuff:) userInfo: nil repeats: YES]; 有定时器的选项,所以它是立即触发,然后每隔5秒?

iOS倒数计时器到特定date

我正试图让倒数计时器到一个特定的date。 我用: -(void) viewWillAppear:(BOOL)animated { NSString *str =@"12/27/2013"; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"MM/dd/yyyy"]; NSDate *date = [formatter dateFromString:str]; NSTimeInterval numberOfSecondsUntilSelectedDate = [date timeIntervalSinceNow]; NSInteger numberOfDays = numberOfSecondsUntilSelectedDate / 86400; startTime = [[NSDate date] retain]; secondsLeft = numberOfDays; NSLog(@"number%f", numberOfSecondsUntilSelectedDate); [self countdownTimer]; } – (void)updateCounter:(NSTimer *)theTimer { if(secondsLeft > 0 ){ secondsLeft — ; […]

在iPhone OS中获取文本的最佳方法是什么?

我想让我的文本框闪烁(就像旧的LCD时钟一样)。 现在,我打电话给无数的NSTimers和select器,等待,改变alpha,等等,然后再改回来。 即使这样,它看起来真的很糟糕,我想我必须把一个NSTimer逐渐改变阿尔法,但从我听到他们不是为了精确的事情。 我的想法是,必须有一种方法来做到这一点,比我目前正在实施更好。 这感觉像黑客。

当应用程序在Swift 2.0后台运行时发送本地通知

我试图在用户最小化应用程序时(通过点击iPhone的主页button或locking手机)发送“推送通知样式”警报。 我的应用程序不断分析一个XML文件(每10秒钟),我希望应用程序继续运行,以便在程序中遇到一些条件时向用户发送本地通知,即使用户已经最小化应用程序或locking了它们电话。 我已经从教程中反弹,每个人似乎都“安排”了一个通知,但是这对我来说不起作用,因为我的通知不是基于时间的,而是基于满足条件的。 到目前为止我所做的是: AppDelegate.swift func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)) return true } MapViewController.swift // Some function func someFunction(delta: Int) { if delta < 100 { // Send alert to user if app is open let […]

如何parsing和提取年,月,日等在iPhone IOS的​​时间间隔

我正在慢慢进入iOS开发,并试图从一个特定的date创build一个向上计时器。 我已经find了代码给了我几秒钟的时间间隔,但我不知道如何从中提取年/月/日/小时/分钟/秒的值,以便在自己的标签中显示每个值作为代码。 到目前为止,我已经知道,以下将给我两秒钟之间的时间间隔,我试图做的是parsing这个,并显示在我看来这是一个股票通过每秒更新UILabel使用NSTimer和调用每1秒select一次,在我的观点上得到这样的东西: 6年10个月13天18小时25分18秒(显然每个标签会随着时间的推移相应更新) NSDate *startDate = [df dateFromString:@"2005-01-01"]; NSTimeInterval passed = [[NSDate date] timeIntervalSinceDate: startDate]; 谢谢

如何暂停NSTimer?

我有一个使用计时器的游戏。 我想这样做,用户可以select一个button,并暂停该计时器,当他们再次点击该button,它将取消暂停该计时器。 我已经有了代码到计时器,只需要一些帮助暂停计时器和双动button。 代码到定时器: -(void)timerDelay { mainInt = 36; timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDownDuration) userInfo:nil repeats:YES]; } -(void)countDownDuration { MainInt -= 1; seconds.text = [NSString stringWithFormat:@"%i", MainInt]; if (MainInt <= 0) { [timer invalidate]; [self delay]; } }

IOS:停止NSTimer

可能重复: NSTimer不停止 我有这个代码: [NSTimer scheduledTimerWithTimeInterval:110.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats:YES]; – (void) targetMethod:(NSTimer) timer{ NSLog(@"Hello World");} 在targetMethod我可以停止计时器与[定时器invalidate],但出于这种方法,我怎样才能停止targetMethod?

如何在Swift中使用NSTimer和NSNotificationCentre来更新UITableViewCells

注意:请在Swift中请求答案。 我在做什么: 桌面单元格每1秒更新一次并显示实时倒计时。 我目前如何做: 我有一个包含标签的单元格的tableview。 当单元格生成时,它们调用一个函数来计算今天和已存储的目标date之间的时间,并显示标签中剩余的倒计时时间。 为了让单元格每秒更新标签,我使用了一个NSTimer ,每秒重新载入tableview。 问题:倒计时是有效的,但是当我尝试做一个Swipe-to-Delete操作时,由于NSTimer重新载入了tableview,它重置了swiped单元,所以不会再被刷新,当然这对于最终用户是不可接受的。 我正在尝试/潜在的解决scheme 我听说有一个更好的方法是使用由NSTimer触发的NSNotificationCenter ,并为每个单元添加侦听器。 每隔1秒从NSNotificationCenter发送“广播”,小区将接收“广播”并更新标签。 我试图添加一个监听器到代码中生成一个新单元的部分中的每个单元格: // Generate the cells override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("Tablecell", forIndexPath: indexPath) as UITableViewCell let countdownEntry = fetchedResultController.objectAtIndexPath(indexPath) as CountdownEntry // MARK: addObserver to "listen" for broadcasts NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateCountdownLabel", name: mySpecialNotificationKey, object: […]

NSTimer改变形象iPhone编程

如何在iPhone编程中使用NSTimer定期更改图像? 我创build一个图像视图来加载图像。 我想在imageview中显示图像,并通过使用NSTimer定期更改图像。

有一个NSTimer运行背景

我已经看到了数百个如何让NSTimer在后台运行的解决scheme。 我知道这是可能的,只要看看像Strava和Runkepper这样的应用程序,可以跟踪你锻炼的时间。 但是,这样做的最佳实践解决scheme是什么? 我无法find一个解决scheme。 另外,我希望NSTimer可以在不同的UIViewControllers中使用。 这是怎么做的最佳做法? 谢谢你的问候! 🙂