nstimer

How to call a method every x seconds in Objective-C using NSTimer?

江枫思渺然 提交于 2019-11-28 21:49:34
问题 I am using Objective-C, Xcode 4.5.1 and working on an app for the iPhone. I have a method A in which I want to call another method B to do a series of calculations every x seconds. In method A I start playing an audio file. Method B will monitor the audio every x seconds for the duration of the audio file. I have found NSTimer as a potential solution, but am having a hard time getting it to work/understanding it. I simply want to call Method B every x seconds and run its calculations, but

How Can I Start And Stop NSTimer?

南楼画角 提交于 2019-11-28 21:07:09
I develop Stop Watch Application. In my application, there are Two UIButtons , StartBtn and StopBtn , And also I use NSTimer . Now, i want to start NSTimer when user click on StartBtn and also stop when your click on StopBtn . I know that NSTimer is stopped by [MyTimerName invalidate]; method but I don't know how to start NSTimer again? The NSTimer class is a bit awkward to use; rather than separating the creation/destruction from the start/stop, it's all rolled together. In other words the timer starts as soon as it's created and stops as soon as it's destroyed. You therefore need to use the

NSTimer vs CACurrentMediaTime()

落花浮王杯 提交于 2019-11-28 20:13:46
问题 So I'm amidst my first iOS game and am struggling with how to go about the best way to integrate object movement. The game relies heavily on fast moving objects and constant, fast user input changes. As such, I'm trying to have object integration and the constraint solver run as quickly and accurately as possible (to minimize user input change in between successive game loop calls). More specifically, I'm unsure of the capabilities of the NSTimer class and CACurrentMediaTime() function. It's

How to stop NStimer event? [duplicate]

柔情痞子 提交于 2019-11-28 18:33:10
Possible Duplicate: NSTimer doesn't stop In my application I am using NStimer to call an animation function every 3 seconds. I want to stop this timer and called another event while the timer is still running. Is this possible? @interface NSTimer *autoTimer; @implementation // Start timer autoTimer = [NSTimer scheduledTimerWithTimeInterval:(3.0) target:self selector:@selector(autoTimerFired:) userInfo:nil repeats:YES]; // Stop timer: [autoTimer invalidate]; autoTimer = nil; First, you want to keep a pointer to the timer self.packetTimer = [NSTimer timerWithTimeInterval:CONNECTION_TIMEOUT

Determine if UIView is visible to the user?

谁说胖子不能爱 提交于 2019-11-28 16:47:40
is it possible to determine whether my UIView is visible to the user or not? My View is added as subview several times into a Tab Bar Controller . Each instance of this view has a NSTimer that updates the view. However I don't want to update a view which is not visible to the user. Is this possible? Thanks mahboudz You can check if: it is hidden, by checking view.hidden it is in the view hierarchy, by checking view.superview != nil you can check the bounds of a view to see if it is on screen The only other thing I can think of is if your view is buried behind others and can't be seen for that

CountDown Timer ios tutorial? [closed]

帅比萌擦擦* 提交于 2019-11-28 16:33:24
I'm making an application where the exams is going on, so when exam start the, time should start with that. For example 30 minutes and it should reduce like 29:59. How can I implement this? Can anyone please give me a sample example or a easy step by step tutorial that i can follow? Adrian P This code is used to create a countdown timer. Code for .h file. @interface UIMyContoller : UIViewController { NSTimer *timer; IBOutlet UILabel *myCounterLabel; } @property (nonatomic, retain) UILabel *myCounterLabel; -(void)updateCounter:(NSTimer *)theTimer; -(void)countdownTimer; @end Code for .m file.

Getting Current Time in string in Custom format in objective c

扶醉桌前 提交于 2019-11-28 16:18:57
I want current time in following format in a string. dd-mm-yyyy HH:MM How? Carl Norum You want a date formatter . Here's an example: NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"dd-MM-yyyy HH:mm"]; NSDate *currentDate = [NSDate date]; NSString *dateString = [formatter stringFromDate:currentDate]; Either use NSDateFormatter as Carl said, or just use good old strftime , which is also perfectly valid Objective-C: #import <time.h> time_t currentTime = time(NULL); struct tm timeStruct; localtime_r(&currentTime, &timeStruct); char buffer[20]; strftime

Weak Reference to NSTimer Target To Prevent Retain Cycle

白昼怎懂夜的黑 提交于 2019-11-28 15:43:06
I'm using an NSTimer like this: timer = [NSTimer scheduledTimerWithTimeInterval:30.0f target:self selector:@selector(tick) userInfo:nil repeats:YES]; Of course, NSTimer retains the target which creates a retain cycle. Furthermore, self isn't a UIViewController so I don't have anything like viewDidUnload where I can invalidate the timer to break the cycle. So I'm wondering if I could use a weak reference instead: __weak id weakSelf = self; timer = [NSTimer scheduledTimerWithTimeInterval:30.0f target:weakSelf selector:@selector(tick) userInfo:nil repeats:YES]; I've heard that the timer must be

How to run timer thought the app entered background or is terminated

左心房为你撑大大i 提交于 2019-11-28 14:46:10
In my app I have a NSTimer, and a selector - (void)TimerCount . I also have a integer int CountNum , and every 0.01 second, it increases by 1 ( CountNum = CountNum + 1 ). Timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(TimerCount) userInfo:nil repeats:YES]; And I want the timer to resume thought the app is terminated or it entered background. How can I do this? If your application is terminated , you will not be able to continue processing. To operate in the background, there are such things as background tasks, but it sounds like your use case does not

Format realtime stopwatch timer to the hundredth using Swift

前提是你 提交于 2019-11-28 13:46:22
I have an app using an NSTimer at centisecond (0.01 second) update intervals to display a running stopwatch in String Format as 00:00.00 (mm:ss.SS). (Basically cloning the iOS built-in stopwatch to integrate into realtime sports timing math problems, possibly needing millisecond accuracy in the future) I use (misuse?) the NSTimer to force-update the UILabel. If the user presses Start, this is the NSTimer code used to start repeating the function: displayOnlyTimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("display"), userInfo: nil, repeats: true) And here