views:

300

answers:

1

I'm trying to simulate the user location animation in MapKit (where-by the user's position is represented by a pulsating blue dot). I've created a custom subclass of MKAnnotationView and in the drawRect method I'm attempting to cycle through a set of colors. Here's a simpler implementation of what I'm doing:

- (void)drawRect:(CGRect)rect {
float magSquared = event.magnitude * event.magnitude;
CGContextRef context = UIGraphicsGetCurrentContext();
if (idx == -1) {
    r[0] = 1.0; r[1] = 0.5; r[2] = 0;
    b[0] = 0; b[1] = 1.0; b[2] = 0.5;
    g[0] = 0.5; g[1] = 0; g[2] = 1.0;
    idx = 0;
}
// CGContextSetRGBFillColor(context, 1.0, 1.0 - magSquared * 0.015, 0.211, .6);
CGContextSetRGBFillColor(context, r[idx], g[idx], b[idx], 0.75);
CGContextFillEllipseInRect(context, rect);
idx++;
if (idx > 3) idx = 0;
}

Unfortunately this just causes the annotations to be one of the 3 different colors and doesn't cycle through them. Is there a way to force the MKAnnotations to continually redraw so that it appears to be animated?

A: 

You are free to call setNeedsDisplay on your annotation view whenever you want it to redraw. The easiest way to do this would be for the annotation view itself to set up a timer that fired every 1/60th of a second or so.

A more sophisticated approach would be to put your drawing code into a custom CALayer and apply a repeating Core Animation animation to it. See my answer to "Animating a custom property of CALayer subclass" for an approach.

Ole Begemann
Thanks. Adding <pre>[NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(setNeedsDisplay) userInfo:nil repeats:YES];</pre> works though I'll definitely look into your CALayer suggestion.
smountcastle