Tag: nstimer

当应用程序处于后台时,向服务器发送经纬度

我经历了这么多的链接,甚至在我还没有find获得经纬度的合适解决scheme之后。 定期iOS背景位置更新 具有“位置”后台模式的iOS长时间运行后台计时器 我尝试了一些链接和论坛,但它只工作了3分钟,然后应用程序根本不更新用户的位置。 – (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: […]

用GCD运行重复NSTimer?

我想知道为什么当你在GCD块中创build一个重复定时器时,它不工作? 这工作正常: -(void)viewDidLoad{ [super viewDidLoad]; [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runTimer) userInfo:nil repeats:YES]; } -(void)runTimer{ NSLog(@"hi"); } 但是这个工作: 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{ NSLog(@"hi"); }

使用NSTimer的秒表不正确地包括暂停时间

这是我的iPhone秒表的代码。 它按预期工作,当点击button时停止并继续。 然而,当我点击“停止”时,计时器不会停止在后台运行,当我点击“开始”恢复时,它会更新时间并跳到当前的位置,而不是从停止的时间恢复。 我怎样才能停止NSTimer ? 是什么导致这种情况发生? @implementation FirstViewController; @synthesize stopWatchLabel; NSDate *startDate; NSTimer *stopWatchTimer; int touchCount; -(void)showActivity { NSDate *currentDate = [NSDate date]; NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate]; NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"mm:ss.SS"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; NSString *timeString=[dateFormatter stringFromDate:timerDate]; stopWatchLabel.text = timeString; [dateFormatter release]; } – (IBAction)onStartPressed:(id)sender […]

NSTimer – 如何在Swift中延迟

我有一个延迟电脑在游戏中移动的问题。 我find了一些解决scheme,但他们不工作在我的情况下,例如 var delay = NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: nil, userInfo: nil, repeats: false) 我试图用这个functionfire但也没有影响。 还有什么其他的可能性?

将parameter passing给Swift中由NSTimer调用的方法

我试图将一个parameter passing给由我的代码中的NSTimer调用的方法。 这是抛出一个例外。 这就是我正在做的。 Circle是我的自定义类。 var circle = Circle() var timer = NSTimer.scheduledTimerWithInterval(1.0, target: self, selector: animate, userInfo: circle, repeats: true) 以下是正在调用的方法 func animate(circle: Circle) -> Void{ //do stuff with circle } 注意:该方法与被调用的类在同一个类中。 所以我相信我已经正确设定了目标。

如何在iOS 7中超过180秒后运行NSTimer?

我已经尝试过,但在iOS 7和Xcode 4.6.2中不能工作超过180秒。 请帮帮我 UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid; UIApplication *app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; }]; NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; -(void) timerMethod{ NSLog(@"in timerMethod"); }

当uiscrollview事件发生时,NSTimer不会被触发

我有一个UIImageView放置在UIScrollView,基本上这个UIImageView拥有非常大的地图,并创build了一个预定义的path与“箭头”指向导航方向的animation。 但是,每当uiscrollevents发生,我认为MainLoop冻结和NSTimer不被解雇,animation停止。 在UIScrollView,CAKeyFrameAnimation或NSTimer中是否有解决这个问题的现有属性? //viewDidLoad self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(drawLines:) userInfo:nil repeats:YES]; – (void)drawLines:(NSTimer *)timer { CALayer *arrow = [CALayer layer]; arrow.bounds = CGRectMake(0, 0, 5, 5); arrow.position = CGPointMake(line.x1, line.y1); arrow.contents = (id)([UIImage imageNamed:@"arrow.png"].CGImage); [self.contentView.layer addSublayer:arrow]; CAKeyframeAnimation* animation = [CAKeyframeAnimation animation]; animation.path = path; animation.duration = 1.0; animation.rotationMode = kCAAnimationRotateAuto; // object auto rotates […]

迅速的NSTimer在背景中

我遇到了很多关于如何在后台处理NSTimer的问题。 我已经尝试了所有的选项之一,实际上是有道理的..当应用程序去背景停止计时器 NSNotificationCenter.defaultCenter().addObserver(self, selector: "appDidEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil) 和 NSNotificationCenter.defaultCenter().addObserver(self, selector: "appDidBecomeActive", name: UIApplicationWillEnterForegroundNotification, object: nil) 起初我以为我的问题解决了,我只是节省了时间,当应用程序进入背景,并计算出应用程序进入前台时的差异。但后来我注意到,时间实际上是推迟了3,4,5秒。 。它实际上是不一样的。我已经比较了另一台设备上的秒表。 有没有真正的任何SOLID解决scheme在后台运行NSTimer?

将一个正在运行的countup显示计时器添加到一个iOS应用程序,如时钟秒表?

我正在使用一个以5秒为单位处理设备动作事件和更新界面的应用程序。 我想添加一个指标,将显示应用程序已经运行的总时间的应用程序。 似乎像秒表一样的计数器,就像本机的iOS时钟应用程序是一个合理的方式来计算应用程序已经运行的时间,并显示给用户。 我不确定的是这种秒表的技术实现。 这是我在想什么: 如果我知道界面更新之间有多长时间,我可以在事件之间加上秒,并将秒数保持为局部variables。 或者,间隔0.5秒的定时器可以提供计数。 如果我知道应用程序的开始date,我可以使用[[NSDate dateWithTimeInterval:(NSTimeInterval) sinceDate:(NSDate *)]将本地variables转换为每个接口更新的date 我可以使用具有短时间样式的NSDateFormatter将更新date转换为使用stringFromDate方法的string 结果string可以分配给界面中的标签。 结果是秒表针对应用程序的每个“打勾”进行更新。 在我看来,这个实现有点太重,不像秒表应用那样stream畅。 有一个更好,更互动的方式来计算应用程序已经运行的时间? 也许iOS已经为此提供了一些东西?

NSTimer timerWithTimeInterval:不工作

在我的项目中实现它之前,我用计时器创build了一个testing应用程序。 这是我第一次使用计时器。 但问题是,当我使用[NSTimer timerWithTimeInterval: target: selector: userInfo: repeats: ];实现了计时器[NSTimer timerWithTimeInterval: target: selector: userInfo: repeats: ]; ,它不工作。 这是我的代码,接口: @interface uialertViewController : UIViewController { NSTimer *timer; } -(void)displayAlert; -(void)hideandview; @end 执行: @implementation uialertViewController – (void)viewDidLoad { [self displayAlert]; [super viewDidLoad]; } -(void)displayAlert{ timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(hideandview) userInfo:nil repeats:NO]; alert = [[UIAlertView alloc] initWithTitle:@"testing" message:@"hi hi […]