[英]Custom Map Callout Animation
我必須創建自定義MKMapView
批注,這些批注看起來與iOS提供的批注幾乎相同,但是具有不同的顏色(白色)且形狀略有不同。
我的第一個嘗試是防止打開原始的黑色UIAnnotationView
並在AnnotationView
的setSelected:animated:
UIAnnotationView
新的子視圖添加到同一UIAnnotationView
。 將其添加到超級視圖后,我已對此視圖進行了動畫處理。 這只有一個問題:注釋中的自定義按鈕不可點擊。
之后,我找到了一個很好的例子:
https://github.com/tochi/custom-callout-sample-for-iOS
當用戶單擊PinAnnotation
時,這會將新的CalloutAnnotation
添加到地圖。 PinAnnotation
s沒有AnnotationView
,只有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;
}
我在此博客上找到了答案: http : //blog.asynchrony.com/2010/09/building-custom-map-annotation-callouts-part-1/
棘手的部分是,您必須在CalloutAnnotationView
的didMoveToSuperView
進行動畫didMoveToSuperView
:)
- (void)didMoveToSuperview
{
[self animateCalloutAppearance:self];
}
我希望它可以幫助其他人:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.