2013-02-17 31 views

回答

3

將它像任何UIView一樣動畫化!

定製內容的子類MKAnnotationView。 不要通過反覆的addAnnotation調用去動畫它!這是錯誤

可以治療anotationView就像任何其他的看法..

這裏是一些代碼,具有圖像的閃爍動畫 - 該代碼應該讓你開始。

(不漂亮,但確切的東西你需要爲你的情況!)

#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 

上傳工作樣例我的Dropbox(它最終會消失,但上面的代碼是所有,但圖像資源和一個iOS應用程序的默認樣板代碼):: https://dl.dropbox.com/u/3753090/test.zip

+0

@valeriy編輯被破壞,因爲我看到它......爲什麼你刪除後的if(!視圖)導致添加動畫發生的大括號x次? – 2013-08-15 14:37:44

+1

您的代碼留下了用舊數據初始化的出列註釋。如果您有一個註釋,此代碼就可以正常工作。但是,如果您有多個相同類型的註釋,當您滾動地圖時,您將遇到麻煩,並且一些註釋將從地圖中消失,並且會出現新的註釋。那些出現的將被取出並重新使用。但是你並沒有用新的數據初始化它們,而是保持原樣。你明白我說的嗎?你的代碼將工作得很好。但請嘗試更改它,以便將註釋大小縮放到實際地圖大小。當您的地圖重新縮放時,您的方法將停止工作。 – 2013-08-15 16:32:36

+0

此處https://github.com/valeriyvan/MissingYandexLocatorAPIiOSExample是示例項目。 ViewController.m使用你的一些代碼。嘗試返回已移除的大括號,您將看到當您重新縮放和重新定位地圖時,多麼奇怪的註釋行爲。 – 2013-08-15 19:16:17