CLLocationManager 标题更新的平滑显示

发布于 2024-12-12 16:50:10 字数 819 浏览 0 评论 0原文

我正在制作一个简单的指南针,它工作得很好,但动画可以更流畅。有时它可能会有点不稳定,类似于帧速率低于标准的游戏。有没有办法让它动画更流畅?

这是我的代码:

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-12-19 16:50:12

您的问题很令人困惑,因为您提到了帧速率较差的游戏,这让我想知道您遇到的是以下三个问题中的哪一个:

  • 您收到如此多的 didUpdateHeading: 消息,表明您实际上遇到了性能问题 (在这种情况下,可能会缓存最后一个值并进行计算以确定是否确实需要更新视图)

  • CoreLocation 实际上是向您发送“跳跃”的数据并使得您的显示看起来跳跃

  • 您还没有为您想要在变换属性上使用的隐式动画正确设置隐式动画参数

我猜您的问题实际上是我列出的第二个问题,并且您从核心位置获取标题更新,该更新从一个位置“跳转”到另一个位置。

我会采取两种方法来解决这个问题 - 一种在另一种之前(因为一种更容易,另一种更难)。

首先,我会尝试稍微增加过渡持续时间,或者至少手动设置它,以便您知道它是什么......也许 2 秒?

[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:2.0f]
                 forKey:kCATransactionAnimationDuration];

needle.transform = CGAffineTransformMakeRotation(radianConst*-h);

[CATransaction commit];

如果做不到这一点,你将不得不采取一些措施并应用某种平滑算法......但是,我相信我听到一位苹果开发人员提到他们已经在他们的核心位置视频之一中做到了这一点。

Your question is confusing because you mention games with poor frame rates, which leads me to wonder which of three problems you're having:

  • You're getting so many didUpdateHeading: messages that you actually are having a performance problem (in which case some caching of the last value and calculation to determine if you really do need to update the view might be in order)

or

  • CoreLocation is actually sending you data that "jumps" around and makes your display look jumpy

or

  • You haven't properly setup the implicit animation parameters for the implicit animation you want to have on the transform property

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?

[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:2.0f]
                 forKey:kCATransactionAnimationDuration];

needle.transform = CGAffineTransformMakeRotation(radianConst*-h);

[CATransaction commit];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文