Tag: nstimer

如何在X秒后更改NSTimer的NSTimeInterval?

我正在快速制作一个有两个定时器的应用程序。 10秒后,我想另一个计时器去更快。 我试过这样做,但它没有工作(我试图改变无功time为1): @IBOutlet var displayTimeLabel: UILabel! var startTimer = NSTimeInterval() var timer = NSTimer() var timer2:NSTimer = NSTimer() var time = 2.0 @IBAction func Start(sender: UIButton) { if !timer2.valid { timer2 = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "updateTime", userInfo: nil, repeats: true) startTimer = NSDate.timeIntervalSinceReferenceDate() } timer = NSTimer.scheduledTimerWithTimeInterval(time, target: self, selector: "timer:", userInfo: nil, […]

dispatch_async UIWebView的loadrequest

我正在加载一个在我的应用程序的JavaScript,脚本将尝试find一个解决scheme,这可能需要一些时间。 我不想等待超过5秒的解决scheme,在这种情况下,我只是想停止请求,并显示一条消息给用户。 我一直试图用NSTimers和dispatch_async来完成,但UIWebView请求仍在使用我的整个主队列,而NSTimer在此期间停止了,等待答案。 请求调用: webViewUIView.delegate = self var path = NSBundle.mainBundle().pathForResource("page", ofType: "html") var url = NSURL(fileURLWithPath: path!) var req = NSURLRequest(URL:url!) var queue = NSOperationQueue() dispatch_async(dispatch_get_main_queue()) { println("********CALLING TO LOADREQUEST") self.webViewUIView.loadRequest(req) } 而我的NSTimer: var timer2 = NSTimer() func webViewDidStartLoad(webView: UIWebView) { println("********Creating the Timer") let aSelector : Selector = "cancelWeb" self.timer2 = NSTimer.scheduledTimerWithTimeInterval(5, target: […]

自定义tableViewCell中的NSTimer

我从viewController激活我的自定义单元类中的function。 自定义单元类看起来像这样: import UIKit class TableViewCell: UITableViewCell { var counter = 10 class func timerStarted(){ var timer = NSTimer() timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "update", userInfo: nil, repeats: true) } class func update(){ let cell = TableViewCell() var count = cell.counter count = –count println(counter) } } 问题是variables计数器不会改变,所以每隔一段时间打印一次。 我怎样才能使它每次都改变价值并倒计时呢? 任何build议,将不胜感激。 编辑:我正在使用一个长按手势识别器来触发该function,这是我不能使用didSelectRowAtIndexPath函数触发它的原因。 我的长按的代码如下所示: func longPressActive(gestureRecognizer:UIGestureRecognizer) […]

UIViewanimation决定中心animation

我正在使用UIView的+ animateWithDuration:delay:options:animations:completion:方法来移动我的观点沿线几秒钟左右的过程。 我想确定,在这个animation的任意时间,UIView是沿着它正在移动的path。 但是,当我这样做,并尝试访问center或frame ,我发现该位置已被设置为它的最终目的地。 我这样做的原因是我有一个NSTimer每隔0.1秒(或左右)发射,它将更新父视图,以显示UIView以前的行。 我希望能够保持这个UIView移动更新。 任何想法如何做到这一点? 我发现的唯一类似的问题是http://forums.macrumors.com/showthread.php?t=1056105 ,但没有显示分辨率。 另外…有没有更好的方法来做到这一点? 谢谢!

继续运行NSTimer切换背景和前景

我正在尝试创build一个NSTimer,由用户在前台手动启动,并且如预期的那样完美地工作: timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateCounter"), userInfo: nil, repeats: true) 其中updateCounter()只是更新UI中的UILabel 。 现在我想实现的是在用户离开应用程序的时候让它运行。 我google了一些东西,发现这个代码(我在Swift中翻译): func applicationDidEnterBackground(application: UIApplication) { var bgTask = UIBackgroundTaskIdentifier() bgTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { () -> Void in UIApplication.sharedApplication().endBackgroundTask(bgTask) } // Start the long-running task and return immediately. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in var vc = TimerViewController() // Fire […]

更新标签在后台运行的计时器

我已经用一个更新标签的NSTimer编码了一个计时器。 问题是,在同一个viewcontroller我有一个uitableview,当我向下滚动,定时器不更新其值,所以用户可以“作弊”停止计时器。 我认为这可以很容易地解决与CGD串行队列,但我不知道如何做到这一点。 提前致谢, 胡安

Xcode:为什么我的计时器在每次打勾时计数2秒?

嗨我有一个计时器,应该从一个iPhone应用程序从12:00分钟到0:00计数! 但是当它开始时,就像这样; 11:58 11:56 11:54 11:52 它每秒钟计数2秒。 这是开始button代码中的代码: tid.text=[NSString stringWithFormat:@"%d:%.2d",minuter,sekunder]; timer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tidklick) userInfo:nil repeats:YES]; 这是方法tidklick: -(void) tidklick { tiden -= 1; sekunder = tiden % 60; minuter= (tiden – sekunder) / 60; tid.text=[NSString stringWithFormat:@"%d:%.2d",minuter,sekunder]; } 这是开始的代码 int tiden=720; int sekunder; int minuter; 这出现在NSLog,我把方法… 2012-04-26 06:41:38.562 Matte [288:f803] tiden: 719 sekunder: 59 2012-04-26 […]

以每x秒的速度更新一个标签

我正在开发我的第一个iPhone应用程序。 我必须每x秒更新一次设备速度的标签。 我已经创build了自己的CLController并且可以获得设备速度,但是我不知道是否必须使用NSTimer更新我的标签。 我该怎么做?

停止并重置NSTimer

我有一个简单的计时器使用,是一个button被激活。 它从60运行到0没有问题,但我想要的是停止和button上重置计时器。 我已经设法停止它,当按下button使用下面的代码,但由于某种原因不能得到它重置,并在60停止。这应该是简单的,但它是行不通的。 有什么build议么? 定时器使用简单的动作设置 – (IBAction)timerStart:(id)sender { if(!secondCounter == 0){ [countdownTimer invalidate]; } else { [self setTimer]; } } 代码定时器 – (void)timerRun { secondCounter = secondCounter – 1; int minutes = secondCounter; NSString *timerOutput = [NSString stringWithFormat:@"%d", minutes ]; countDownLabel.text = timerOutput; if(secondCounter == 0){ [countdownTimer invalidate]; countdownTimer = nil; } } – (void)setTimer […]

在iOS中self.timer = nil vs 之间有什么区别?

任何人都可以解释我self.timer=nil vs [self.timer invalidate] ? 什么发生在self.timer的内存位置? 在我的代码中 self.timer=nil 不停止计时器 [self.timer invalidate] 停止计时器。 如果你需要我的代码,我也会更新。