Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I create a custom "pin-drop" animation using MKAnnotationView?
    text
    copied!<p>I have an instance of <code>MKMapView</code> and would like to use custom annotation icons instead of the standard pin icons supplied by MKPinAnnotationView. So, I've setup a subclass of MKAnnotationView called CustomMapAnnotation and am overriding <code>-(void)drawRect:</code> to draw a CGImage. This works.</p> <p>The trouble comes when I try to replicate the <code>.animatesDrop</code> functionality supplied by MKPinAnnotationView; I would love for my icons to appear gradually, dropped from above and in left-to-right order, when the annotations are added to the <code>MKMapView</code> instance.</p> <p>Here is -(void)drawRect: for CustomMapAnnotation, which works when you just draw the UIImage (which is what the 2nd line does):</p> <pre><code>- (void)drawRect:(CGRect)rect { [super drawRect:rect]; [((Incident *)self.annotation).smallIcon drawInRect:rect]; if (newAnnotation) { [self animateDrop]; newAnnotation = NO; } } </code></pre> <p>The trouble comes when you add the <code>animateDrop</code> method:</p> <pre><code>-(void)animateDrop { CGContextRef myContext = UIGraphicsGetCurrentContext(); CGPoint finalPos = self.center; CGPoint startPos = CGPointMake(self.center.x, self.center.y-480.0); self.layer.position = startPos; CABasicAnimation *theAnimation; theAnimation=[CABasicAnimation animationWithKeyPath:@"position"]; theAnimation.fromValue=[NSValue valueWithCGPoint:startPos]; theAnimation.toValue=[NSValue valueWithCGPoint:finalPos]; theAnimation.removedOnCompletion = NO; theAnimation.fillMode = kCAFillModeForwards; theAnimation.delegate = self; theAnimation.beginTime = 5.0 * (self.center.x/320.0); theAnimation.duration = 1.0; [self.layer addAnimation:theAnimation forKey:@""]; } </code></pre> <p>It just doesn't work, and there could be a lot of reasons why. I won't get into all of them now. The main thing I am wanting to know is if the approach is sound at all, or if I should try something entirely different.</p> <p>I tried also to package up the whole thing into an animation transaction so that the beginTime parameter might actually work; this seemed to not do anything at all. I don't know if this is because I am missing some key point or whether it's because MapKit is trashing my animations somehow.</p> <pre><code> // Does nothing [CATransaction begin]; [map addAnnotations:list]; [CATransaction commit]; </code></pre> <p>If anyone has any experience with animated MKMapAnnotations like this, I'd love some hints, otherwise if you can offer CAAnimation advice on the approach, that'd be great too.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload