我做了一個演示項目(移動-MKAnnotationView在github上演示)用於在地圖上的下行駛的汽車是其鏈接汽車(譯註)動畫(如Uber應用)不工作
https://github.com/pratikbhiyani/Moving-MKAnnotationView
編輯我的代碼基於給定答案的vinaut,但仍然問題是,當我們縮放或滾動地圖動畫時,我們縮放或滾動地圖註釋集合到原始角度一段時間,在ios 7和ios 6中分散注意力。
下面是我的演示項目
下面的屏幕截圖是一些代碼,我改變
- (void) setPosition : (id) posValue;
{
NSLog(@"set position");
//extract the mapPoint from this dummy (wrapper) CGPoint struct
MKMapPoint mapPoint = *(MKMapPoint*)[(NSValue*)posValue pointerValue];
CLLocationCoordinate2D coord = MKCoordinateForMapPoint(mapPoint);
CGPoint toPos;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
toPos = [self.mapView convertCoordinate:coord toPointToView:self.mapView];
}
else
{
CGFloat zoomFactor = self.mapView.visibleMapRect.size.width/self.mapView.bounds.size.width;
toPos.x = mapPoint.x/zoomFactor;
toPos.y = mapPoint.y/zoomFactor;
}
[self setTransform:CGAffineTransformMakeRotation([self getHeadingForDirectionFromCoordinate:MKCoordinateForMapPoint(previousPoint) toCoordinate: MKCoordinateForMapPoint(mapPoint)])];
if (MKMapRectContainsPoint(self.mapView.visibleMapRect, mapPoint)) {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [NSValue valueWithCGPoint:self.center];
animation.toValue = [NSValue valueWithCGPoint:toPos];
animation.duration = 1.0;
animation.delegate = self;
animation.fillMode = kCAFillModeForwards;
//[self.layer removeAllAnimations];
[self.layer addAnimation:animation forKey:POSITIONKEY];
//NSLog(@"setPosition ANIMATED %x from (%f, %f) to (%f, %f)", self, self.center.x, self.center.y, toPos.x, toPos.y);
}
self.center = toPos;
previousPoint = mapPoint;
}
我的目標是相同的動車像Uber應用。
那麼問題是什麼?移動地圖時,註釋消失了嗎? –
是的。並沒有顯示出它在ios 6中顯示的流暢動畫; –
嘗試使用翻譯變換而不是重新定位... – k06a