首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >dispatch_after CLLocationManager didUpdateLocations或didFailWithError

dispatch_after CLLocationManager didUpdateLocations或didFailWithError
EN

Stack Overflow用户
提问于 2014-05-18 20:38:47
回答 2查看 428关注 0票数 0

我有这段代码来管理更新设备位置的"pull to refresh“:

代码语言:javascript
复制
[self.glassScrollView.foregroundScrollView addPullToRefreshWithActionHandler:^{

    int64_t delayInSeconds = 1.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        NSLog(@"Pulled");
        [weakSelf.locationManager startUpdatingLocation];
        [weakForegroundScrollView.pullToRefreshView stopAnimating];
    });
}];

当它开始更新位置时,动画将停止,但不会在实际更新位置或返回错误时停止。这造成了一个尴尬的情况,它似乎已经更新了位置和UI (旋转的轮子动画消失了),但它仍然通过CLLocationManagerDelegate方法处理它。

那么,将locationManager:didUpdateLocationslocationManager:didFailWithError方法“连接”到这个队列的最佳方式是什么呢?我在考虑在上面的代码中添加某种类型的“侦听器”,以等待某个方法在CLLocationManagerDelegate方法中被调用。

这也是我的locationManager:didUpdateLocations方法。

代码语言:javascript
复制
(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{
    NSLog(@"didUpdateLocations");
    CLLocation *currentLocation = [locations lastObject];
    // do stuff in the UI

    // I stop updating the location to save battery
    [self.locationManager stopUpdatingLocation];
}

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2014-05-18 23:04:47

好的。下面的代码可能会有帮助:

代码语言:javascript
复制
-(void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(whenCompleted) name:@"whenCompleted" object:nil];
}

-(void)viewWillDisappear:(BOOL)animated{
  [super viewWillDisappear:animated];
  [[NSNotificationCenter defaultCenter] removeObserver:self name:@"whenCompleted" object:nil];
}

-(void)whenCompleted{
  [weakForegroundScrollView.pullToRefreshView stopAnimating];
}

执行完所有方法后,在最后一个方法结束之前,发布通知:-

代码语言:javascript
复制
[[NSNotificationCenter defaultCenter]postNotificationName:@"whenCompleted" object:nil];

然后,当进度完成时,它将stopAnimating

票数 0
EN

Stack Overflow用户

发布于 2014-05-18 23:44:59

您可以使用类似以下内容来处理拉取请求:

代码语言:javascript
复制
[self.glassScrollView.foregroundScrollView addPullToRefreshWithActionHandler:^{

    int64_t delayInSeconds = 1.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        NSLog(@"Pulled");
        [weakSelf.locationManager startUpdatingLocation];
        [weakForegroundScrollView.pullToRefreshView stopAnimating];
    });
}];

我假设延迟一秒是因为您希望隐藏在一秒后刷新的拉动,即使刷新仍在进行中。

我的建议是:

代码语言:javascript
复制
[self.glassScrollView.foregroundScrollView addPullToRefreshWithActionHandler:^{

    [weakSelf.locationManager startUpdatingLocation];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC);, dispatch_get_main_queue(), ^(void){
        [weakSelf.glassScrollView.foregroundScrollView setContentOffset:CGPointZero animated:YES];
    });
}];

这会使UIActivityIndicatorView保持旋转,但会将其滚动到屏幕之外。如果你再把它拉下来,你会看到它还在运行。这是保持活动指示器运行的一种简单方法,但让它远离用户的方式。

然后,正如里奇所说,只有当位置被检索到时,你才会stopAnimating (或者在[NSNotificationCenter defaultCenter]上发布一个本地通知,这样你的观察者就可以这样做)。但不要只是尝试在一段时间后执行stopAnimating,而是将其与您正在执行的任何异步任务的实际完成情况绑定在一起。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23721985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档