nstimer

Having a NSTimer running background

拟墨画扇 提交于 2019-12-02 13:27:57
I've seen hundreds of solutions of how to get a NSTimer to run in the background. I know that it is possible, just look at apps like Strava and Runkepper that tracks your time when working out. But what is the best practice solution for doing so? I can't find one unison solution for this. Also, I would like the NSTimer to be used across different UIViewControllers. How is this done as a best practice? Thanks in regards! :) NSTimers don't run in the background. Store the current time and the elapsed time of the timer when you got the background. When you come back to the foreground, you set up

Running NSTimer Within an NSThread?

妖精的绣舞 提交于 2019-12-02 13:05:58
问题 I am trying to run a timer in the background of my application, i am using the timer heavily in my application and i'd rather running it in the background, However i am getting memory leaks when trying to release the NSAoutreleasePool. My Timer class is singleton, so if i start new timer the old timer get dealloc it . + (void)timerThread{ timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(startTimerThread) object:nil]; //Create a new thread [timerThread start]; //start the

NSTimer

懵懂的女人 提交于 2019-12-02 11:23:55
1. block 示例代码: @property (nonatomic, strong) NSTimer *timer; // 1. 初始化定时器 self.timer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) { }]; // 2. 将定时器加入RunLoop [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode]; // 3. 将定时器移出RunLoop [self.timer invalidate]; 引用关系图: 引用 引用 UIViewController NSTimer NSRunLoop 来源: https://blog.csdn.net/suma110/article/details/102755277

How do I find the time interval remaining from NSTimer

不问归期 提交于 2019-12-02 10:41:06
I have set a NSTimer.scheduledTimerWithTimeInterval method which has an interval every 20 minutes. I want to be able to find out how much time is left when the the app goes into background mode. How do I find out how much time is left from the interval? Thanks You have access to a NSTimer's fireDate , which tells you when the timer is going to fire again. The difference between now and the fireDate is an interval you can calculate using NSDate's timeIntervalSinceDate API . E.G. something like: let fireDate = yourTimer.fireDate let nowDate = NSDate() let remainingTimeInterval = nowDate

UIView animation determine center during animation

╄→гoц情女王★ 提交于 2019-12-02 09:01:31
问题 I'm using UIView's + animateWithDuration:delay:options:animations:completion: method to move my view along a line over the course of a few seconds or so. I would like to determine, at an arbitrary time during this animation, where the UIView is along with path it is moving. However when I do this and try and access center or frame , I find that the location is already set to it's final destination. The reason I'm doing this is that I've got an NSTimer firing every 0.1 seconds (or thereabouts)

Using NSTimer in a Loop

拥有回忆 提交于 2019-12-02 07:54:24
I need to show a timer counting down from 30 to 0, several times. (30 to 0, start over at 30, etc) However when I placed it in a for-loop, instead of waiting till the timer invalidates to begin the next timer, the loop iterates through and several timers are created. How can I form a loop that waits until the timer has invalidated? - (IBAction)startCountdown:(id)sender { NSNumber *rounds = [[NSNumber alloc]initWithInt:sliderRounds.value]; for (int i = rounds.intValue; i > 0; i--) { [self timer]; } You can use the scheduledTimerWithTimeInterval [NSTimer scheduledTimerWithTimeInterval: 0.5

Multiple NSTimers slows down the Application

一曲冷凌霜 提交于 2019-12-02 07:32:44
I am using multiple NStimers into my cocoa (MAC) application and i have added all of them in NSRunLoopCommonModes separately like below: NSRunLoop *runloop = [NSRunLoop currentRunLoop]; updateServerTimeTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateServerTime) userInfo:nil repeats:YES]; [runloop addTimer:updateServerTimeTimer forMode:NSRunLoopCommonModes]; But after some time, my app. gets hung and also UI responds late. It is also slowing down my MAC, Can anyone provide a better approach to use multiple timers continuously within an application 来源:

Running NSTimer Within an NSThread?

≡放荡痞女 提交于 2019-12-02 05:05:50
I am trying to run a timer in the background of my application, i am using the timer heavily in my application and i'd rather running it in the background, However i am getting memory leaks when trying to release the NSAoutreleasePool. My Timer class is singleton, so if i start new timer the old timer get dealloc it . + (void)timerThread{ timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(startTimerThread) object:nil]; //Create a new thread [timerThread start]; //start the thread } //the thread starts by sending this message + (void) startTimerThread { timerNSPool = [

Xcode: Why is my timer counting 2 seconds on every tick?

六眼飞鱼酱① 提交于 2019-12-02 05:04:50
问题 Hi I have a timer that should count from 12:00 minutes to 0:00 in an iphone app! But when it starts it counts like this; 11:58 11:56 11:54 11:52 it is counting 2 seconds on every tick. this is the code in the start button code: tid.text=[NSString stringWithFormat:@"%d:%.2d",minuter,sekunder]; timer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tidklick) userInfo:nil repeats:YES]; This is the method tidklick: -(void) tidklick { tiden -= 1; sekunder = tiden % 60;

Threaded NSTimer

我是研究僧i 提交于 2019-12-02 04:41:29
I am aware of the many questions regarding this topic, as I myself have asked one previously however, my issue now seems to be more related to the threading part. I have the following 2 methods. -(void) restartTimer { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; self.timer = [NSTimer scheduledTimerWithTimeInterval:1. target:self selector:@selector(dim) userInfo:nil repeats:YES]; time = 31; NSLog(@"calling restart timer"); [self performSelectorOnMainThread:@selector(timerImageUpdate) withObject:nil waitUntilDone:NO]; [[NSRunLoop currentRunLoop] addTimer:self.timer forMode