2013-07-11 58 views
1

我必须创建自定义MKMapView注释,它与iOS提供的注释几乎相同,但具有不同的颜色(白色)和稍微不同的形状。自定义地图标注动画

我第一次尝试是为了防止打开原始黑UIAnnotationView S和在AnnotationViewsetSelected:animated:增加了一个新的子视图相同UIAnnotationView。将它添加到超级视图后,我已将该视图动画化。这只有一个问题:注释中的自定义按钮不可点击。

在此之后,我发现这个很好的例子:

https://github.com/tochi/custom-callout-sample-for-iOS

这增加了一个新的CalloutAnnotation到地图,当用户点击一个PinAnnotationPinAnnotation s没有AnnotationView s,只有CalloutAnnotation s没有。

这工作正常,除了我无法弄清楚如何在注释视图创建时像iOS一样初始缩放动画。

我想用动画是以下(在这里发现了SO和工作的罚款与我第一次尝试):

- (void)animateCalloutAppearance:(CalloutAnnotationView *)annotaitonView 
{ 
    self.endFrame = annotaitonView.frame; 
    CGFloat scale = 0.001f; 
    annotaitonView.transform = CGAffineTransformMake(scale, 0.0f, 0.0f, scale, [self xTransformForScale:scale andAnnotation:annotaitonView], [self yTransformForScale:scale]); 

    [UIView animateWithDuration:0.075f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
     CGFloat scale = 1.2f; 
     annotaitonView.transform = CGAffineTransformMake(scale, 0.0f, 0.0f, scale, [self xTransformForScale:scale andAnnotation:annotaitonView], [self yTransformForScale:scale]); 
    } completion:^(BOOL finished) { 
     [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ 
      CGFloat scale = 0.90; 
      annotaitonView.transform = CGAffineTransformMake(scale, 0.0f, 0.0f, scale, [self xTransformForScale:scale andAnnotation:annotaitonView], [self yTransformForScale:scale]); 
     } completion:^(BOOL finished) { 
      [UIView animateWithDuration:0.075 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ 
       CGFloat scale = 1.0; 
       annotaitonView.transform = CGAffineTransformMake(scale, 0.0f, 0.0f, scale, [self xTransformForScale:scale andAnnotation:annotaitonView], [self yTransformForScale:scale]); 
      } completion:nil]; 
     }]; 
    }]; 
} 

#pragma mark - The helper methods 

- (CGFloat)relativeParentXPosition:(CalloutAnnotationView *)annotationView { 
    return annotationView.bounds.size.width/2.0f; 
} 

- (CGFloat)xTransformForScale:(CGFloat)scale andAnnotation:(CalloutAnnotationView *)annotationView { 
    CGFloat xDistanceFromCenterToParent = self.endFrame.size.width/2.0f - [self relativeParentXPosition:annotationView]; 
    CGFloat transformX = (xDistanceFromCenterToParent * scale) - xDistanceFromCenterToParent; 

    return transformX; 
} 

- (CGFloat)yTransformForScale:(CGFloat)scale { 
    CGFloat yDistanceFromCenterToParent = self.endFrame.size.height/2.0f; 
    CGFloat transformY = yDistanceFromCenterToParent - yDistanceFromCenterToParent * scale; 

    return transformY; 
} 

回答