汽车(注释)动画(像超级应用程序)无法正常工作

时间:2013-10-09 09:29:18

标签: iphone ios objective-c mkmapview mkannotationview

我制作了一个演示项目(来自github上的Moving-MKAnnotationView演示),用于在地图上移动汽车以下是其链接

https://github.com/pratikbhiyani/Moving-MKAnnotationView

我根据vinaut的给定答案编辑我的代码,但仍然问题是,当我们缩放或滚动地图动画时,在我们缩放或滚动地图注释设置为ios 7和ios 6时会分散注意力它的原始角度一段时间。

以下是我的演示项目的屏幕截图

enter image description here

以下是我更改的一些代码

- (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;
}

我的目标是像在超级应用中一样移动汽车。

2 个答案:

答案 0 :(得分:14)

CLCoordinate2D / MKMapPoint / CGPoint的转换函数似乎发生了变化......

Detecting a point in a MKPolygon broke with iOS7 (CGPathContainsPoint)

注释消失,因为MkMapPoints和CGIPoints之间的转换不再起作用,如果您记录CALayer的“位置”,您将获得视图外的点。不知道为什么它在做触摸事件时有效。

如果您将功能更改为:

    - (void) setPosition : (id) posValue; 
{
    //extract the mapPoint from this dummy (wrapper) CGPoint struct
    MKMapPoint mapPoint = *(MKMapPoint*)[(NSValue*)posValue pointerValue];  
    CLLocationCoordinate2D coord = MKCoordinateForMapPoint(mapPoint);

    CGPoint toPos = [self.mapView convertCoordinate:coord toPointToView:self.mapView];


    if (MKMapRectContainsPoint(self.mapView.visibleMapRect, mapPoint)) {

        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];

        animation.fromValue = [NSValue valueWithCGPoint:self.center];
        animation.toValue = [NSValue valueWithCGPoint:toPos];   
        animation.duration = 0.8;
        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;


}

它应该再次运作。

答案 1 :(得分:14)

我是Moving-MKAnnotationView(https://github.com/100grams/Moving-MKAnnotationView.git)的原始撰稿人。该组件最初是使用iOS4.3编写的,自那以后发生了很多变化。 :-)

这里的根本原因是从MKMapPoint到CGPoint的转换(屏幕坐标)。虽然之前的代码工作正常,但它在iOS7上中断,我通过使用它将lat / lon坐标转换为屏幕坐标来修复它:

convertCoordinate:toPointToView:

我已将此修复程序以及其他一些更新提交给https://github.com/100grams/Moving-MKAnnotationView.git,因此它现在适用于iOS7 / Xcode5。