【问题标题】:How to animate map annotation movement correctly?如何正确动画地图注释运动?
【发布时间】:2014-09-07 19:22:40
【问题描述】:

我正在MKMapView 上显示城市公交车的实时位置。我的应用程序以一定的间隔轮询位置并在地图上更新它们。我正在尝试为地图注释的移动设置动画。

我已经使用从this stackoverflow answer 找到的以下代码成功地动画运动:

- (void)moveBusAnnotation:(TKLBus*)bus coordinate:(CLLocationCoordinate2D)coordinate {
    [UIView animateWithDuration:0.5f
    animations:^(void){
        bus.annotation.coordinate = coordinate;
    }];
}

问题是当用户在播放动画时平移或缩放地图时,注释的运动路径看起来很奇怪且有问题。这是一个演示:

请注意地图注释如何遵循一条奇怪的曲线而不是直线。 公交车运动是模拟的,所以忽略它在地图上的奇怪位置。

如何使动画看起来更自然或在地图平移/缩放时停止动画?

编辑:动画似乎做了正确的事情。看起来很奇怪,因为地图注释的下一个坐标在动画播放时正在移动。我认为解决方案是在用户触摸屏幕时阻止动画。

【问题讨论】:

    标签: ios iphone animation ios7 map


    【解决方案1】:

    试试这个:

    设置一个布尔值来指示用户是否正在使用地图:

    @property (nonatomic, assign) BOOL userIsInteracting;
    

    然后检查用户在地图中的触摸:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [super touchesBegan:touches withEvent:event];
    
        for (UITouch * touch in touches) 
        {
            CGPoint loc = [touch locationInView:_mapView];
            if ([_mapView pointInside:loc withEvent:event]) 
            {
                _userIsInteracting = YES;
                break;
            }
        }
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [super touchesEnded:touches withEvent:event];
    
        for (UITouch * touch in touches) 
        {
            CGPoint loc = [touch locationInView:_mapView];
            if ([_mapView pointInside:loc withEvent:event]) 
            {
                _userIsInteracting = NO;
                break;
            }
        }
    }
    

    现在您知道何时为地图中的位置设置动画了:

    - (void)moveBusAnnotation:(TKLBus*)bus coordinate:(CLLocationCoordinate2D)coordinate 
    {
        if (_userIsInteracting) return;
    
        [UIView animateWithDuration:0.5f
                         animations:^(void){
                         bus.annotation.coordinate = coordinate;
                         }];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-25
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 2014-03-12
      相关资源
      最近更新 更多