Search code examples
iphonedelegatesmkmapviewmkoverlay

Animate viewForOverlay MKCircleView


I want to animate the circle that I'm adding as an MKOverLay. I want it to drop from the top of the screen. How do I animate through the delegate method? Or is it in the addition of the overlay? Can anyone point me in the right direction? Thanks!

I have this in the mapView Delegate Method

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{

    if([overlay isKindOfClass:[MKCircle class]]) {
        // Create the view for the radius overlay.
        MKCircleView *circleView = [[MKCircleView alloc] initWithOverlay:overlay];
        circleView.strokeColor = [UIColor whiteColor];
        circleView.fillColor = [[UIColor blueColor] colorWithAlphaComponent:0.2];

        return circleView;
    }

    return nil;
}

And I add overLays like so:

        MKCircle *circle = [MKCircle circleWithCenterCoordinate:userCoord radius:200];
        [mainMapView addOverlay:circle];

Solution

  • Let's say you want to have a scale up + fade in animation, you might do this:

    @interface MyMapOverlayView : UIView<CAAction>
    @end
    
    @implementation MyMapOverlayView
    
    - (id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event
    {
        if ( [ event isEqualToString:@"onOrderIn" ] )
        {
            return self ;
        }
    
        return [ super actionForLayer:layer forKey:event ] ;
    }
    
    - (void)runActionForKey:(NSString *)event object:(id)anObject arguments:(NSDictionary *)dict
    {
        if ( [ event isEqualToString:@"onOrderIn" ] )
        {
            {
                CABasicAnimation * anim = [ CABasicAnimation animationWithKeyPath:@"transform" ] ;
                anim.fromValue = [ NSValue valueWithCATransform3D:CATransform3DMakeScale( 0.1f, 0.1f, 1.0f ) ] ;
                [ self.layer addAnimation:anim forKey:nil ] ;
            }
    
            {
                CABasicAnimation * anim = [ CABasicAnimation animationWithKeyPath:@"opacity" ] ;
                anim.fromValue = @0.0f ;
                [ self.layer addAnimation:anim forKey:nil ] ;
            }
        }
    }
    
    @end
    

    n.b.: I took a guess at how you might add these animations at the time your overlay is added to the map view, but I haven't tested it. You will need to experiment. You might be able to just set them when the view is created.

    update: I tested it, it seems to work.