nstimer

Weak Reference to NSTimer Target To Prevent Retain Cycle

怎甘沉沦 提交于 2019-11-27 19:52:24
问题 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

Using an NSTimer in Swift

血红的双手。 提交于 2019-11-27 19:36:48
In this scenario, timerFunc() is never called. What am I missing? class AppDelegate: NSObject, NSApplicationDelegate { var myTimer: NSTimer? = nil func timerFunc() { println("timerFunc()") } func applicationDidFinishLaunching(aNotification: NSNotification?) { myTimer = NSTimer(timeInterval: 5.0, target: self, selector:"timerFunc", userInfo: nil, repeats: true) } } Ryan You can create a scheduled timer which automatically adds itself to the runloop and starts firing: Swift 2 NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "timerDidFire:", userInfo: userInfo, repeats: true)

Run repeating NSTimer with GCD?

别说谁变了你拦得住时间么 提交于 2019-11-27 18:35:11
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 scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runTimer) userInfo:nil repeats:YES]; }); } -(void)runTimer

method not called with dispatch_async and repeating NSTimer

≡放荡痞女 提交于 2019-11-27 18:26:03
问题 I am developing an app where i want to call method in separate queue using dispatch_async . I want to call that method repeatedly after certain interval of time. But the method is not getting called. I don't know whats wrong. Here is my code: dispatch_async( NotificationQueue, ^{ NSLog(@"inside queue"); timer = [NSTimer scheduledTimerWithTimeInterval: 20.0 target: self selector: @selector(gettingNotification) userInfo: nil repeats: YES]; dispatch_async( dispatch_get_main_queue(), ^{ // Add

NSTimer - how to delay in Swift

六眼飞鱼酱① 提交于 2019-11-27 18:09:33
I have a problem with delaying computer's move in a game. I've found some solutions but they don't work in my case, e.g. var delay = NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: nil, userInfo: nil, repeats: false) I tried to use this with function fire but also to no effects. What other possibilities there are? valfer Swift 3 With GCD: let delayInSeconds = 4.0 DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delayInSeconds) { // here code perfomed with delay } or with a timer: func myPerformeCode() { // here code to perform } let myTimer : Timer = Timer

Creating a StopWatch in Iphone

倾然丶 夕夏残阳落幕 提交于 2019-11-27 17:03:42
问题 I am trying to create a simple stopwatch. I referred to this website ( http://www.apptite.be/tutorial_ios_stopwatch.php) to do this app. When I click the start button, I get -596:-31:-23:-648 and the stopwatch does not run. The code looks like: ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController{ UILabel *lbl; NSTimer *stopTimer; NSDate *startDate; } @property (strong,nonatomic) IBOutlet UILabel *lbl; -(IBAction)startPressed:(id)sender; -(IBAction)stopPressed

What is the best way to make a bouncing ball animation with infinite loop on iPhone?

喜欢而已 提交于 2019-11-27 17:03:33
问题 I am developing an iPhone game in which birds bounce. I have set up the images for animating the wings of the flying bird like this: [imgBird[i] setAnimationImages:birdArrayConstant]; [imgBird[i] setAnimationDuration:1.0]; [imgBird[i] startAnimating]; Now how I make the bird move is to use an NSTimer to fire every 0.03 seconds which adds/subtracts 1 from the x or y coordinate from imgBird[i].center. I learnt about doing it like this from here. http://icodeblog.com/2008/10/28/iphone

How to pass an argument to a method called in a NSTimer

吃可爱长大的小学妹 提交于 2019-11-27 15:14:20
问题 I have a timer calling a method but this method takes one paramether: theTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(timer) userInfo:nil repeats:YES]; should be theTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(timer:game) userInfo:nil repeats:YES]; now this syntax doesn't seems to be right. I tried with NSInvocation but I got some problems: timerInvocation = [NSInvocation

How keep NSTimer when application entering background?

谁都会走 提交于 2019-11-27 15:09:13
问题 I'm here because a didn't find any solutions for my issue :( I'm doing an simple application in which i have to send (by socket) some informations to a server (like GPS l/L, accuracy, Battery level, etc). The current code works fine when application is in foreground. myTimer = [NSTimer scheduledTimerWithTimeInterval: 2.0 target:self selector: @selector(sendPosToServer:) userInfo:nil repeats: YES]; myTimer2 = [NSTimer scheduledTimerWithTimeInterval: 3.0 target:self selector: @selector

Accuracy of NSTimer

大城市里の小女人 提交于 2019-11-27 14:31:15
I am trying to use NSTimer to create a Stop-watch style timer that increments every 0.1 seconds, but it seems to be running too fast sometimes .. This is how I've done it: Timer =[NSTimer scheduledTimerWithTimeInterval: 0.1 target:self selector:@selector(updateTimeLabel) userInfo:nil repeats: YES]; and then: -(void)updateTimeLabel { maxTime=maxTime+0.1; timerLabel.text =[NSString stringWithFormat:@"%.1f Seconds",maxTime]; } This will display the value of the timer in the Label, and I can later utilize maxTime as the time when the Timer is stopped ... THe problem is that it runs very