Tag: nstimer

Swift – scheduledTimer秒表小时部分不工作

忍受我一个星期前,我拿起了Swift和Xcode(我创build了一个应用程序,在我的假期期间学习Swift) 我使用scheduledTimer创build了一个计时器来充当秒表。 秒和分钟运行良好,但我不能得到的工作时间。 我究竟做错了什么? currentTime = 0 timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in self.currentTime += 1 let hoursPortion = String(format: "%02d", CGFloat(self.currentTime) * 60) let minutesPortion = String(format: "%02d", self.currentTime / 60) let secondsPortion = String(format: "%02d", self.currentTime % 60) //let tenPortion = String(format: "%d", self.currentTime * 10 % 10) self.TimerDisplay.text = […]

IOS位置更新与NSTimer

我想每秒钟获得一次位置更新。 这个时间间隔对于我想创build的animation非常重要,以保持真实的速度。 我明白,[myLocationManager startUpdatingLocation]将自动轮询“didUpdateToLocations”,但它不是完全每秒一次。 有什么方法可以使用NSTimer每秒钟准确地获得我的位置? 谢谢你,jj

如何在特定点停止NSTimer?

我创build了一个简单的button游戏,每按一下button就可以给用户一个点。 该button每1.5秒随机出现在屏幕上。 我希望游戏在30秒后或20个随机popupbutton后结束。 我一直在使用下面的代码在屏幕上随意popupbutton: timer = [NSTimer scheduledTimerWithTimeInterval: 1.5 target:self selector:@selector(moveButton:) userInfo:nil repeats:YES]; 我已经在头文件中声明了计时器: NSTimer *timer; @property (nonatomic, retain) NSTimer *timer; 我已阅读使用计时器的 Apple文档,但未能完全理解它。 我想也许我可以使用: – (void)countedTimerFireMethod:(NSTimer *)timer{ count ++; if(count > 20){ [self.timer invalidate]; self.timer = nil; 但它不能正常工作。 我究竟做错了什么? 我是新来的Objective-C,所以我不熟悉如何工作。

while循环使用NSTimer

我想创build一个运行的NSTimer可以说10分钟。 然后我想在延迟10分钟的时间之后写一个while循环,之后执行这行。 例如。 NSTimer * countDown = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector userInfo:nil repeats:NO]; while (countDown == stil running (hasnt reached 10 minute mark) ){ // kill 10 minutes of time }//when 10 minutes is up execute next line of code

iOS使用NSTimers向select器发送参数

有没有办法通过NSTimer发送参数给select器? myTimer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(removeTheNote:) userInfo:nil repeats:NO]; – (void)removeTheNote:(NSString*)note { NSLog(@"Note %@ ———– REMOVED!",note); } 我知道使用: myTimer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(removeTheNote:myNote) userInfo:nil repeats:NO]; 不起作用,所以我问,有没有办法做到这一点?

计时器离开应用程序,并返回到iOS

目前在我的iOS应用程序中,当用户退出到主屏幕并返回到应用程序时,它会请求在我的AppDelegate中设置的login凭据。 但是我想要做的是,如果用户退出应用程序,并在例如2分钟内,定时器重置,用户不需要input他的密码。 当用户在2分钟后回到应用程序时,会提醒他再次input密码。 任何帮助将不胜感激。 谢谢!

如何重置NSTimer的触摸,触摸移动在iOS

我正在努力的应用程序,我必须在3秒后隐藏控制button,所以我写代码使用NSTimer和触摸开始,它的工作,但问题是,当我再次触摸任何另一个button比我的计时器不重置,即使我移动我的触摸示例,如拖动。 如果我拖动或移动触摸它应该重置计时器,但它不会。 我发现这个实现工作,如果我不断触摸其他领域(但它不是在控制button上工作)如果我不断触摸控制button仍然隐藏3秒后如何解决这个问题。 我希望事件发生在button点击也。 编辑我解决了我自己的问题..我已经添加此代码button点击部门,它的工作原理.. 感谢您的支持 if(screenTimer) { [screenTimer invalidate]; screenTimer = nil; screenTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(turnOffScreen) userInfo:nil repeats:NO]; } 这是我的代码 // Touch began for touch event. – (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if(screenTimer) { [screenTimer invalidate]; screenTimer = nil; } mode1View.hidden=NO; screenTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(turnOffScreen) userInfo:nil repeats:NO]; } – (void)turnOffScreen{ […]

为什么我的时间UILabels从暂停状态恢复播放时跳过2秒?

我有一个奇怪的问题使用MPMoviePlayerController 。 我有一个currentTimeLabel表示当前的播放时间, endTimeLabel表示剩余的播放时间。 使用NSTimer每1秒就会触发更新标签的function。 这与video播放时UISlider增加的UISlider一致。 但是,问题是,随着video播放,我点击暂停button,当我再次点击播放button时,我的currentTimeLabel和endTimeLabel从上次停止的地方跳过2秒。 以下是提供的代码: override func viewDidLoad() { super.viewDidLoad() print("viewDidLoad") NSNotificationCenter.defaultCenter().addObserver( self, selector: "moviePlayerPlaybackStateDidChangeNotification:", name: MPMoviePlayerPlaybackStateDidChangeNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver( self, selector: "moviePlayerLoadStateDidChangeNotification:", name: MPMoviePlayerLoadStateDidChangeNotification, object: nil) setUpVideoPlayer() isPlaybackSliderTouched = false isPauseButtonTouched = false } override func viewDidAppear(animated: Bool) { super.viewDidAppear(true) print("viewDidAppear") timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updatePlaybackSlider", userInfo: nil, repeats: […]

IOS,如何处理应用程序启动时的多个本地通知?

我正在创build一个使用计时器的应用程序。 假设用户可以设置多个定时器, 对于每个定时器,应用程序安排一个本地通知。 当应用程序运行在前台或在后台我没有问题处理多个本地通知。 我的问题是,当用户设置多个计时器, 然后终止应用程序 (双击主页button,closures应用程序)。 在这种情况下,当计时器到期时,所有相关的本地通知都显示为横幅,应用程序图标徽章增加。 所以我想要处理所有这些通知时,用户从通知横幅启动应用程序或点击应用程序图标,但使用 didFinishLaunchingWithOptions 我只能处理一个通知 [launchOptions UIApplicationLaunchOptionsLocalNotificationKey] 我需要处理所有计时器的所有本地通知! 我怎样才能做到这一点?

Swift NSTimer将用户信息检索为CGPoint

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { let touch = touches.anyObject() as UITouch let touchLocation = touch.locationInNode(self) timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "shoot", userInfo: touchLocation, repeats: true) // error 1 } func shoot() { var touchLocation: CGPoint = timer.userInfo // error 2 println("running") } 我正在尝试创build一个计时器,该计时器将周期性地运行,将触摸点(CGPoint)作为userInfo传递给NSTimer,然后在shoot()函数中访问它。 但是,现在我得到一个错误,说 1)调用中的额外参数select器 2)不能转换expression式typesAnyObject? 到CGPoint 现在我似乎无法将userInfo传递给另一个函数,然后检索它。