2013-10-07 100 views
2

我知道一般應如何在此基礎上SO問題動畫圓形路徑等CALayers: iPhone - How to make a circle path for a CAKeyframeAnimation?動畫效果GMSMarker圓形路徑

然而,GMSMarkerLayer是CALayers的一個特殊的子類,它似乎並沒有迴應「位置」鍵路徑(按照該鏈接中的指令做任何事情,我可以看到),而是將響應「緯度」和「經度」的關鍵路徑。

下面是我試過的代碼:

CAKeyframeAnimation *circlePathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
CGMutablePathRef circularPath = CGPathCreateMutable(); 
CGRect pathRect = CGRectMake(marker.position.latitude, marker.position.longitude, 0.001, 0.001); 
CGPathAddEllipseInRect(circularPath, NULL, pathRect); 
circlePathAnimation.path = circularPath; 
circlePathAnimation.duration = 1.0f; 
circlePathAnimation.repeatCount = HUGE_VALF; 

[marker.layer addAnimation:circlePathAnimation forKey:[NSString stringWithFormat:@"circular-%@", marker.description]]; 
CGPathRelease(circularPath); 

由於關鍵幀動畫將使用「位置」的keyPath,我怎麼能轉換到這2個獨立keypaths(經度和緯度),所以我可以動畫在地圖上的圓圈標記?

任何幫助,非常感謝。

+0

問題,你有興趣在地球上畫一個圓或者在屏幕上畫一個圓圈嗎?如果您對地球上的測地線動畫感興趣,那麼手動插入一系列短線段就是您的前進之路。如果您有興趣在屏幕上爲圈子設置動畫效果,然後在https://code.google.com/p/gmaps-api-issues/issues/entry?template=Maps%上提出動畫「偏移量」的功能請求20SDK%20for%20iOS%20-%20Feature%20Request – Brett

+0

Hi @Brett感謝您的提問。我想要在地球表面的圓形路徑上使標記動起來。有趣的是,GMSMarkerLayer不允許我使用「position」keypath並直接將其轉換爲「緯度」和「經度」,因爲這會立即解決我的問題。插入一系列短線段可以工作,但要求我使用各自的鍵路徑分別對lat和lon進行動畫處理,您是否知道是否有更有效的方法去處理這些問題? – Jamornh

+1

請隨時通過https://code.google.com/p/gmaps-api-issues/issues/entry?template=Maps%20SDK%20for%20iOS%20-%20Feature%20Request提出功能要求 – Brett

回答

4

由於目前我無法使用「位置」keypath進行動畫製作,因此我最終使用「緯度」和「經度」關鍵路徑單獨進行了動畫處理。

首先計算點並將它們添加到2個單獨的數組中,一個用於緯度值(y)和一個用於經度(x),然後使用CAKeyFrameAnimation中的values屬性進行動畫處理。創建2個CAKeyFrameAnimation對象(每個軸1個)並使用CAAnimationGroup將它們組合在一起並將它們一起動畫以形成一個圓。

在我的等式中,我改變每個軸上半徑的長度,這樣我也可以生成一條橢圓路徑。

NSMutableArray *latitudes = [NSMutableArray arrayWithCapacity:21]; 
    NSMutableArray *longitudes = [NSMutableArray arrayWithCapacity:21]; 
    for (int i = 0; i <= 20; i++) { 
     CGFloat radians = (float)i * ((2.0f * M_PI)/20.0f); 

     // Calculate the x,y coordinate using the angle 
     CGFloat x = hDist * cosf(radians); 
     CGFloat y = vDist * sinf(radians); 

     // Calculate the real lat and lon using the 
     // current lat and lon as center points. 
     y = marker.position.latitude + y; 
     x = marker.position.longitude + x; 


     [longitudes addObject:[NSNumber numberWithFloat:x]]; 
     [latitudes addObject:[NSNumber numberWithFloat:y]]; 
    } 

    CAKeyframeAnimation *horizontalAnimation = [CAKeyframeAnimation animationWithKeyPath:@"longitude"]; 
    horizontalAnimation.values = longitudes; 
    horizontalAnimation.duration = duration; 

    CAKeyframeAnimation *verticleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"latitude"]; 
    verticleAnimation.values = latitudes; 
    verticleAnimation.duration = duration; 

    CAAnimationGroup *group = [[CAAnimationGroup alloc] init]; 
    group.animations = @[horizontalAnimation, verticleAnimation]; 
    group.duration = duration; 
    group.repeatCount = HUGE_VALF; 
    [marker.layer addAnimation:group forKey:[NSString stringWithFormat:@"circular-%@",marker.description]];