IOS Custom MKAnnotation object resembling MKUserLocation animation
1 answer
Animate it like any UIView!
subclass MKAnnotationView for custom content. don't go through repeated calls to AddAnnotation for animation! this is not true
you can treat anotationView just like any other view.
here is some code that has flashing image animations - this code should start.
(not really, but the exact material you'll need for your case!)
#import "DDViewController.h"
#import <MapKit/MapKit.h>
#import <QuartzCore/QuartzCore.h>
@interface DummyAnnotation : NSObject<MKAnnotation>
@end
@implementation DummyAnnotation
- (CLLocationCoordinate2D)coordinate { return CLLocationCoordinate2DMake(51, 10); }
- (NSString *)title { return @"Dummy"; }
@end
@interface DummyAnnotationView : MKAnnotationView
@end
@implementation DummyAnnotationView
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
imageView.animationImages = @[[UIImage imageNamed:@"1.gif"],[UIImage imageNamed:@"2.gif"]];
imageView.animationDuration = 5;
[imageView startAnimating];
[self addSubview:imageView];
return self;
}
@end
@interface DDViewController() <MKMapViewDelegate>
@end
@implementation DDViewController
- (MKMapView*)mapView {
return (MKMapView*)self.view;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//init a region
MKCoordinateRegion region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(51.0, 10.0), MKCoordinateSpanMake(2.0, 2.0));
[self.mapView setRegion:region animated:NO];
//add a dumy pin
DummyAnnotation *ann = [[DummyAnnotation alloc] init];
[self.mapView addAnnotation:ann];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if([annotation isKindOfClass:[DummyAnnotation class]]) {
DummyAnnotationView *view = (id)[mapView dequeueReusableAnnotationViewWithIdentifier:@"animated"];
if(!view)
view =[[DummyAnnotationView alloc ] initWithAnnotation:annotation reuseIdentifier:@"animated"];
view.bounds = CGRectMake(0, 0, 59, 59);
view.backgroundColor = [UIColor purpleColor];
//
//Animate it like any UIView!
//
CABasicAnimation *theAnimation;
//within the animation we will adjust the "opacity"
//value of the layer
theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
//animation lasts 0.4 seconds
theAnimation.duration=0.4;
//and it repeats forever
theAnimation.repeatCount= HUGE_VALF;
//we want a reverse animation
theAnimation.autoreverses=YES;
//justify the opacity as you like (1=fully visible, 0=unvisible)
theAnimation.fromValue=[NSNumber numberWithFloat:1.0];
theAnimation.toValue=[NSNumber numberWithFloat:0.1];
//Assign the animation to your UIImage layer and the
//animation will start immediately
[view.layer addAnimation:theAnimation forKey:@"animateOpacity"];
return view;
}
return nil;
}
//---
@end
uploaded a working sample to my Dropbox (it will go away eventually, but the above code is just image assets and default iOS app boilerplate code) :: https://dl.dropbox.com/u/3753090/test.zip
+3
source to share