【发布时间】:2015-01-27 18:52:15
【问题描述】:
我正在构建一个使用 GeoFencing 的应用程序。我的 didEnterRegion 和 didExitRegion 方法按预期调用,看起来顺序正确,但未能相应地更新 UI。
什么有效:
- 用户进入区域
- didEnterRegion 被调用
- UI 在该方法中正确更新
什么不起作用:
- 用户进入区域
- 用户直接从一个地区到另一个地区
- didExitRegion 被调用
- 界面更新
- didEnterRegion 被调用
- NSLogs 表明一切都以正确的顺序执行
- UI 未更新。在 didExitRegion 中完成的 UI 更新仍然存在。
我的方法:
更新标签的自定义函数(从 didEnterRegion 和 didExitRegion 调用):
-(void)updateCurrentLocationLabelAndImage:(NSString *)locationText subLocationText:(NSString *)subLocationText;
{
// Clear existing animations before we begin
[self.locationLabel.layer removeAllAnimations];
[self.subLocationLabel.layer removeAllAnimations];
[self.ovalImageView.layer removeAllAnimations];
if (![locationText isEqual:@""])
{
// Only animate if the text changes
if (![self.locationLabel.text isEqualToString:locationText])
{
// Update the ovalImageView
CGSize maxLabelSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - (([UIScreen mainScreen].bounds.size.width * 0.0267) * 2)), 64); // maximum label size
float expectedLabelWidth = [locationText boundingRectWithSize:maxLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:self.locationLabel.font } context:nil].size.width; // get expected width
CGFloat xValueForImage = ((([UIScreen mainScreen].bounds.size.width - expectedLabelWidth) / 2) - 25); // Calcuate the x-coordinate for the ovalImageView
if (xValueForImage < 15)
{
xValueForImage = 15; // we don't want it to display off-screen
}
self.ovalImageViewLeadingConstraint.constant = xValueForImage;
[self.view setNeedsUpdateConstraints];
[self changeLabelText:self.subLocationLabel string:subLocationText];
// Update the subLocationLabel
[UIView animateWithDuration:.15 animations:^{
// Animate
self.locationLabel.alpha = 0;
self.ovalImageView.alpha = 1;
[self.view layoutIfNeeded]; // update the UI
}completion:^(BOOL finished) {
// Set the text
self.locationLabel.text = locationText;
self.locationLabel.adjustsFontSizeToFitWidth = YES;
[UIView animateWithDuration:.15 animations:^{
// Animate
self.locationLabel.alpha = 1;
}completion:^(BOOL finished) {
// Complete
}];
}];
}
} else if ([locationText isEqual:@""])
{
// Move it to the center
self.ovalImageViewLeadingConstraint.constant = (([UIScreen mainScreen].bounds.size.width / 2) - 9); // Default center calculation for this image
[self.view setNeedsUpdateConstraints];
[self changeLabelText:self.subLocationLabel string:subLocationText]; // Update the subLocationLabel
[UIView animateWithDuration:.15 animations:^{
self.locationLabel.alpha = 0;
self.ovalImageView.alpha = 1;
[self.view layoutIfNeeded];
}completion:^(BOOL finished) {
// Complete
self.locationLabel.text = @"";
}];
}
}
但标签保持不变,即使它记录了正确的执行顺序并且一切看起来都很好。有任何想法吗?我真的被困住了..
谢谢!
【问题讨论】:
-
日志“Entered work region ...”从何而来?
-
您进入区域 2 的事件会在退出区域 1 后立即覆盖该值。尝试查看两个不同标签的值并检查..
-
@NaveenPrasadR 我现在添加的测试标签设置正确
-
@NaveenPrasadR 你对我能做些什么来解决这个问题有什么想法吗?
-
它按预期工作。你想实现的功能是什么?
标签: ios objective-c location geofencing clregion