我必须创建自定义MKMapView注释,它们看起来与iOS提供的注释几乎相同,但具有不同的颜色(白色)和略微不同的形状。
我的第一个尝试是阻止打开原始的黑色UIAnnotationView,然后在AnnotationView的setSelected:animated:中添加了一个新的子视图到同一个UIAnnotationView中。在将这个视图添加到superview之后,我对它进行了动画处理。这只有一个问题:我在注释中的自定义按钮不可点击。
在这之后,我找到了这个很好的例子:
https://github.com/tochi/custom-callout-sample-for-iOS
当用户单击PinAnnotation时,这会将新的CalloutAnnotation添加到地图中。PinAnnotation%s没有AnnotationView%s,只有CalloutAnnotation%s有。
这很好用,除了我不知道如何在创建注释视图时在注释视图上做类似iOS的初始缩放动画。
我想要使用的动画如下(在这里找到,在我的第一次尝试中效果很好):
- (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;
}发布于 2013-07-12 04:11:30
我在这个博客上找到了答案:http://blog.asynchrony.com/2010/09/building-custom-map-annotation-callouts-part-1/
棘手的部分是你必须在CalloutAnnotationView的didMoveToSuperView中做动画:)
- (void)didMoveToSuperview
{
[self animateCalloutAppearance:self];
}我希望它能帮助其他人:)
https://stackoverflow.com/questions/17600623
复制相似问题