1

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

我的第一次尝试是阻止打开原来的黑色sUIAnnotationView并在其中添加了一个新的子视图。在将它添加到超级视图后,我已经为这个视图设置了动画。这只有一个问题:注释中的自定义按钮不可点击。AnnotationViewsetSelected:animated:UIAnnotationView

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

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

CalloutAnnotation当用户单击 时,这会在地图中添加一个新的PinAnnotationPinAnnotations没有AnnotationViews,只有CalloutAnnotations有。

这很好用,除了我不知道在创建注释视图时如何在注释视图上执行类似 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;
}
4

1 回答 1

2

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

棘手的部分是你必须在CalloutAnnotationView's中做动画didMoveToSuperView:)

- (void)didMoveToSuperview
{
    [self animateCalloutAppearance:self];
}

我希望它可以帮助别人:)

于 2013-07-11T20:11:30.470 回答