nstimer

NSTimer.scheduledTimerWithTimeInterval in Swift Playground

倖福魔咒の 提交于 2019-12-29 08:38:07
问题 All the examples I've seen on using the "NSTimer.scheduledTimerWithTimeInterval" within Swift show using the "target: self" parameter, but unfortunately this doesn't work in Swift Playgrounds directly. Playground execution failed: <EXPR>:42:13: error: use of unresolved identifier 'self' target: self, Here's an example referenced above that results in the error: func printFrom1To1000() { for counter in 0...1000 { var a = counter } } var timer = NSTimer.scheduledTimerWithTimeInterval(0, target:

Run repeating NSTimer with GCD?

北城以北 提交于 2019-12-28 05:10:16
问题 I was wondering why when you create a repeating timer in a GCD block it doesen't work? This works fine: -(void)viewDidLoad{ [super viewDidLoad]; [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runTimer) userInfo:nil repeats:YES]; } -(void)runTimer{ NSLog(@"hi"); } But this doesent work: dispatch_queue_t myQueue; -(void)viewDidLoad{ [super viewDidLoad]; myQueue = dispatch_queue_create("someDescription", NULL); dispatch_async(myQueue, ^{ [NSTimer

How to stop NSTimer

谁说胖子不能爱 提交于 2019-12-28 04:00:08
问题 Hey so i am just making an example application and i am having a little trouble, So i have a table view and then i have a few rows and when a user clicks a row it takes them to a new view. On this view i have a button to play music. I am using a timer to make a slider bar increase based on music duration and remaining time. Now my problem is, what do i have to put so that when i go back to the table view via the top left button, that the NSTimer stops? this is what i have so far, i cannot get

UIScrollView pauses NSTimer until scrolling finishes

假如想象 提交于 2019-12-27 10:38:55
问题 While a UIScrollView (or a derived class thereof) is scrolling, it seems like all the NSTimers that are running get paused until the scroll is finished. Is there a way to get around this? Threads? A priority setting? Anything? 回答1: An easy & simple to implement solution is to do: NSTimer *timer = [NSTimer timerWithTimeInterval:... target:... selector:.... userInfo:... repeats:...]; [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; 回答2: For anyone using Swift 3 timer =

UIScrollView pauses NSTimer until scrolling finishes

不打扰是莪最后的温柔 提交于 2019-12-27 10:38:22
问题 While a UIScrollView (or a derived class thereof) is scrolling, it seems like all the NSTimers that are running get paused until the scroll is finished. Is there a way to get around this? Threads? A priority setting? Anything? 回答1: An easy & simple to implement solution is to do: NSTimer *timer = [NSTimer timerWithTimeInterval:... target:... selector:.... userInfo:... repeats:...]; [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; 回答2: For anyone using Swift 3 timer =

runloop相关

人走茶凉 提交于 2019-12-26 02:55:51
runloop的相关类: CFRunLoop CFRunLoopMode CFRunLoopTimer CFRunLoopSource CFRunLoopObserve 每一个线程都对应一个runloop 主线程对应的runloop系统自动帮我们创建好了 子线程的runloop需要我们自己手动创建 关于mode: runloop每次只能在一种模式下运行: 模式model有5种:(前2种常用,后3种不怎么使用) NSRunLoopDefaultMode UITrackingRunLoopMode . . . 比如说:定时器设置在default模式下,如果滚动拖曳textview,(切换)启用的是UITrackingRunLoopMode ,此时添加在default模式下的定时器不起作用 为了在滚动模式下定时器也可以使用:可以把定时器添加在占位common模式下,或者把定时器分别添加在2个模式下,让2个模式中都有定时器,这样不管切换到哪个模式下,都可以正常使用 关于timer: NSTimer sche........这个方法启用的定时器可以直接使用 NSTimer*timer=[NSTimer timerin......] 创建对象产生的NSTimer对象,就必须添加到runloop的一种mode模式中才可以正常使用 关于source: source0:是指接收用户触摸的事件源

Resuming an NSTimer in Swift

馋奶兔 提交于 2019-12-25 18:41:28
问题 The other questions I've seen on this didn't help. I want to be able to start, pause, and then resume the NSTimer. I know how to start the NSTimer. I also know that you can't 'pause' the NSTimer but you can invalidate it. But what would I have to do if I wanted to resume it keeping the same time I had when I stopped the timer? Here's the code: var startTimer = NSTimeInterval() var timer = NSTimer() @IBOutlet var displaylabel: UILabel! @IBAction func start(sender: UIButton) { timer = NSTimer

Swift: NSTimer automatically ends without counting down

拜拜、爱过 提交于 2019-12-25 10:02:11
问题 Swift newbie here. I have two questions: First, how do I create a timer which AUTOMATICALLY counts down when a ViewController scene is opened? My problem is that the NSTimer ENDS automatically when the scene is opened. For instance, whenever I go to the said scene, the TimerLabel says: “Time’s Up!” Before my second question, below is my tweaked code from: makeapppie.com/…/swift-swift-using-nstimer-to-make-a-timer…/ var timer = NSTimer() let timeInterval:NSTimeInterval = 0.05 let timerEnd

Background location update every n minutes

梦想的初衷 提交于 2019-12-25 09:26:32
问题 I'm writing an application that requires location updates every n minutes and sends data to a server even when the application is in background mode. I have gone through so many links about this task. As I found the proper solution is to use a timer and make it as a background task. Concerning to this I have two questions: How can I implement these regular background location updates? I understand that Apple allows background tasks for an only certain time. So how can I make long-running

Timer on swift not working

↘锁芯ラ 提交于 2019-12-25 09:10:07
问题 I have a function working with a button: @IBAction func btnSiguiente(_ sender: Any) { if indexImagenes == imagenes.count { indexImagenes = 0 } escaparate.image = NSImage(named: imagenes[indexImagenes]) indexImagenes += 1 } I want to make it work with a Timer: var playTimer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(autoplay), userInfo: nil, repeats: true) func autoplay() { if indexImagenes == imagenes.count { indexImagenes = 0 } escaparate.image = NSImage