CLLocationManager 标题更新的平滑显示
我正在制作一个简单的指南针,它工作得很好,但动画可以更流畅。有时它可能会有点不稳定,类似于帧速率低于标准的游戏。有没有办法让它动画更流畅?
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
CLLocationManager* lm = [[CLLocationManager alloc] init];
self.locman = lm;
self.locman.delegate = self;
self.locman.headingFilter = 2;
[self.locman startUpdatingHeading];
}
- (void) locationManager:(CLLocationManager *)manager
didUpdateHeading:(CLHeading *)newHeading {
CGFloat h = newHeading.magneticHeading;
NSString* dir = @"N";
NSArray* cards = [NSArray arrayWithObjects:
@"N", @"NE", @"E", @"SE", @"S", @"SW", @"W", @"NW", nil];
for (int i = 0; i < 8; i++)
if (h < 45.0/2.0 + 45*i) {
dir = [cards objectAtIndex: i]; break;
}
needle.transform = CGAffineTransformMakeRotation(radianConst*-h);
direction = dir;
}
I'm making a simple compass, and it works just fine but the animation could be smoother. At times it can be a bit jumpy, similar to a game with sub-par frame rate. Is there a way to make it animate more smoothly?
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
CLLocationManager* lm = [[CLLocationManager alloc] init];
self.locman = lm;
self.locman.delegate = self;
self.locman.headingFilter = 2;
[self.locman startUpdatingHeading];
}
- (void) locationManager:(CLLocationManager *)manager
didUpdateHeading:(CLHeading *)newHeading {
CGFloat h = newHeading.magneticHeading;
NSString* dir = @"N";
NSArray* cards = [NSArray arrayWithObjects:
@"N", @"NE", @"E", @"SE", @"S", @"SW", @"W", @"NW", nil];
for (int i = 0; i < 8; i++)
if (h < 45.0/2.0 + 45*i) {
dir = [cards objectAtIndex: i]; break;
}
needle.transform = CGAffineTransformMakeRotation(radianConst*-h);
direction = dir;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题很令人困惑,因为您提到了帧速率较差的游戏,这让我想知道您遇到的是以下三个问题中的哪一个:
或
或
我猜您的问题实际上是我列出的第二个问题,并且您从核心位置获取标题更新,该更新从一个位置“跳转”到另一个位置。
我会采取两种方法来解决这个问题 - 一种在另一种之前(因为一种更容易,另一种更难)。
首先,我会尝试稍微增加过渡持续时间,或者至少手动设置它,以便您知道它是什么......也许 2 秒?
如果做不到这一点,你将不得不采取一些措施并应用某种平滑算法......但是,我相信我听到一位苹果开发人员提到他们已经在他们的核心位置视频之一中做到了这一点。
Your question is confusing because you mention games with poor frame rates, which leads me to wonder which of three problems you're having:
or
or
I'm guessing your problem is actually the second one I listed, and that you're getting heading updates from core location which "jump" from location to location.
There are two approaches I'd take to this problem - one before the other (because one is easier and the other is harder).
First, I'd try to increase the transition duration slightly, or, at least set it manually so you know what it is... maybe 2 seconds?
Failing that, you'll have to some up with and apply some sort of smoothing algorithm... however, I believe I heard an apple developer mention they did this already in one of their Core Location videos.