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

pra*_*ani 25 iphone objective-c mkmapview mkannotationview ios

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

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;
}
Run Code Online (Sandbox Code Playgroud)

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

vin*_*aut 14

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

用iOS7检测MKPolygon中的一个点(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;


}
Run Code Online (Sandbox Code Playgroud)

它应该再次工作.

  • 嘿朋友,你接近回答所以+50为你:) (3认同)

100*_*ams 14

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

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

convertCoordinate:toPointToView:
Run Code Online (Sandbox Code Playgroud)

我已经将此修复程序以及其他一些更新提交到https://github.com/100grams/Moving-MKAnnotationView.git,因此它现在可以在iOS7/Xcode5上运行.