自定义地图标注动画

时间:2013-07-11 18:20:35

标签: iphone objective-c annotations mkmapview

我必须创建自定义MKMapView注释,它们与iOS提供的注释几乎相同,但颜色(白色)和形状略有不同。

我的第一次尝试是阻止打开原始的黑色UIAnnotationView,并在AnnotationView setSelected:animated:为同一个UIAnnotationView添加了新的子视图。将此视图添加到superview后,我已将其设置为动画。这只有一个问题:注释中的自定义按钮无法点击。

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

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

当用户点击CalloutAnnotation时,会向地图添加新的PinAnnotationPinAnnotation没有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;
}

1 个答案:

答案 0 :(得分:2)

我在这个博客上找到了答案:http://blog.asynchrony.com/2010/09/building-custom-map-annotation-callouts-part-1/

棘手的部分是你必须在CalloutAnnotationView的{​​{1}}中做动画:)

didMoveToSuperView

我希望它可以帮助其他人:)