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;
}
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.