Tag: nstimer

NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats不调用该方法

计时器从不调用该方法。 我究竟做错了什么 ? 这是代码: NSTimer *manualOverlayTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(hideManual) userInfo:nil repeats:NO]; 方法: -(void)hideManual 谢谢

当我更改时间间隔的值时,如何更新我的NSTimer

我有一个NSTtimer实施,它工作正常。 我也有连接到iPhone的UISlider的时间间隔参数。 但是,当我改变它的值时, NSTimer仍然在原来的时间间隔运行,它不会被更新。 我怎样才能实现一个NSTimer ,并改变它的时间间隔,因为我的UISlider的值发生了变化。 以下是我用于NSTimer 。 [NSTimer scheduledTimerWithTimeInterval:mySlider.value target:self selector:@selector(myMethod) userInfo:nil repeats:YES]; 我希望它不断地用UISlider的值更新它的时间间隔。

迅速无效计时器不起作用

我有这个问题几天了,我不明白我做错了什么。 我的应用程序基本上只是创build一些定时器。 我需要阻止他们,创造新的。 但目前阻止他们不起作用。 self.timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval, target:self, selector: "timerDidEnd:", userInfo: "Notification fired", repeats: false) 这是我的计时器 func timerDidEnd(timer:NSTimer){ createUnrepeatedAlarmWithUpdateInterval() } 因为我的计时器不想停止我正在使用不重复的计时器,并停止后自己启动它。 func stopAlarm() { if self.timer != nil { self.timer!.invalidate() } self.timer = nil self.timer = NSTimer() } 这就是我如何阻止我的计时器。 alarmManager.stopAlarm() alarmManager.createUnrepeatedAlarmWithUpdateInterval() 我在创build一个新的定时器之前调用stopAlarm()函数。 我真的不知道我在做什么错,所以我感谢每一个答案:) class AlarmManager: ViewController{ private var timer : NSTimer? private var unrepeatedTimer : […]

删除NSTimer / UITableViewCell实时?

我有一个UITableView ,我在每个UITableViewCell上设置了许多定时器。 每个计时器从用户在我的应用程序上创build“发布”开始,并应在24小时内过期。 但是,我想这样,当所有24小时结束时, UITableViewCell实时删除自己,但我似乎无法弄清楚我应该删除计时器的时间或地点。 我有一个方法,将不断刷新计时器每秒使用NSTimer.scheduledTimerWithTimeInterval ,它NSTimer.scheduledTimerWithTimeInterval更新每个UITableViewCell上的计时器。 但是,我无法find一个方法或find如何find每个UITableViewCell中的每个计时器是否完成。 很明显,我可以find如果计时器是在viewDidLoad完成,但只有在视图变为活动状态时调用。 有没有我缺less的方法或任何我可以用来查找是否通过scheduledTimerWithTimeInterval方法计时器完成,如果是,删除它? 这里是我的代码如下: //I have a self.offers array declared in the beginning of my class whcih will act as the UITableView data source. var offers = [Offer]() func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { //Dequeue a "reusable" cell let cell = tableView.dequeueReusableCellWithIdentifier(offerCellIdentifier) as! OfferCell setCellContents(cell, […]

位置更新在后台和暂停的应用程序模式

我想写的应用程序,每5分钟发送到服务器的低精度位置为它的两种模式:前景和背景。 因为我不想减less电池充电,并且位置信号变化只给出高精度值,所以我每5分钟就从GPS开始/停止位置更新。 应用程序从前台模式工作正常,但从后台模式只工作约1小时(并停止发送位置后)。 我想我在backgroundTask / NSTimer代码丢失的东西,因为我是新的iOS。 我会非常感谢你的帮助。 该应用程序将只适用于iOS 7及以上。 一般来说,我的algorithm如下: ** start backGroundTask ** [_locationManager startUpdatingLocation] ** handle received location in "didUpdateLocations:" listener ** [_locationManager stopUpdatingLocation] ** create new thread and fire the NSTimer with delay 5 min ** end backGroundTask 这是我的代码: – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self startLocationManager]; // .. other init app […]

将计时器设置为UIPageViewController

我试图让UIPageViewController每5秒浏览页面。 如何在Swift中做到这一点? 有没有什么办法显式调用viewControllerAfterViewController ? var timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector("update"), userInfo: nil, repeats: true) func update() -> UIViewController { // can u please tell me what to add here } func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? { var index = (viewController as tourViewControllerhelper).pageIndex if (index == 0) || (index == NSNotFound) […]

为NSTimer添加暂停function

首先,这里是一些Xcode码表的工作代码。 我有两个button, Start和Stop 。 按下button时,它们的标题会改变。 现在我想添加一个暂停function。 我知道有很多关于这个的线索,但是(我不知道为什么),我无法得到它的工作。 那么在我的代码中实现这个function的最好方法是什么? 我已经尝试过使用暂停date,并从我的NSTimeInterval减去它,但得到负值… 非常感谢! 所以我这样做了: //use timer to update the ui only, store start date (NSDate) and time interval elapsed (NSTimeInterval) //this is called when the timer is not running, nor paused – a sort of 'first run' function -(void)onTimerStart { //zero the time elapsed time_passed = 0; //save […]

iOS5 ARC可以安排从后台select器安排NSTimers吗?

我正在尝试debugging我的应用程序。 我在我的非弧代码中使用了一些NSTimer实例(从主线程): [NSTimer scheduledTimerWithTimeInterval:5 target:musicPlayer selector:@selector(playPause:) userInfo:nil repeats:NO]; 这工作正常,如果我将此代码分配给一个button,然后单击一个button。 定时器启动。 我也试过: if( self.deliveryTimer == nil) { self.deliveryTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(playPause:) userInfo:nil repeats:NO]; } -(void)playPause:(NSTimer*)timer { [deliveryTimer invalidate]; deliveryTimer = nil; //more code here } 我期望定时器执行,按下面的播放/暂停方法,然后转为零,所以我可以稍后重置计时器。 我检查nil的原因是因为我有3个不同的代码path,可能会设置计时器。 每个人都有一个NSLog语句指出计时器已经被调度。 我的代码运行,我看到计时器正在计划,但他们似乎并没有在正常的应用程序执行过程中触发。 我正在调查为什么。 短期定时器,使用相同的逻辑火罚款。 当我让应用程序运行一段时间,我遇到了问题。 NSTimers可以被ARC收回吗? 如果我从performSelectorInBackground设置计时器,这有什么关系吗? 当我写这个问题的时候,我注意到我的一些定时器是通过被调用的代码path创build的: [self performSelectorInBackground:@selector(notifyDelegateOfDataPoint:) withObject:data]; 背景select器是否可以成为我的计时器之前不会触发/恢复的原因? 任何帮助表示赞赏,这个bug已经扰乱了我超过2周! 更新: 在更改代码以使用 NSTimers 的主线程后, […]

如何在Objective-C / iOS中创build一个精确的计时器事件?

我期待在iOS上为SMPTE时间码(HH:MM:SS:FF)创build一个倒数计时器。 基本上,这只是一个分辨率为33.33333ms的倒数计时器。 我不太确定NSTimer是否足够准确,可以指望触发事件来创build此计时器。 每次这个计时器增加/减less时,我想发起一个事件或者调用一段代码。 我是Objective-C的新手,所以我正在从社区寻求智慧。 有人提出了CADisplayLink类,寻找一些专家的意见。

如何检查NSTimer是否已经失效

我有一个NSTimer对象,如果用户点击button或者退出视图,我需要使其失效。 所以我有: [myNSTimer invalidate]; 在我的button处理程序和里面viewWillDisappear 。 如果用户点击一个button,然后存在一个视图,应用程序会抛出一个exception,因为myNSTimer已经失效。 我在viewWillDisappear方法中需要做的是检查myNSTimer是否失效。 我怎么做? 我试过了: if(myNSTimer != nil) [myNSTimer invalidate]; 但是这并没有奏效。