禁用在为其框架设置动画时向mapView添加注释

Toy*_*dor 5 annotations objective-c mkmapview ios

我使用UIView动画为我的mapView框架制作动画:

     [UIView animateWithDuration:0.25f animations:^{
           self.mapView.frame = frame;
        } completion:nil];
}
Run Code Online (Sandbox Code Playgroud)

在使用上面的内容扩展mapView时,我看到mapView正在添加错误的注释,直到动画结束.动画完成后,放错位置的注释会消失.

展开地图时,调用如下:

  1. animateWithDuration开始
  2. MapView类:regionWillChangeAnimated
  3. MapView类:didAddAnnotationViews
  4. MapView类:regionDidChangeAnimated
  5. animateWithDuration完成

我最小化地图时没问题可能是因为没有调用mapView:didAddAnnotationViews.

我可以在动画时以某种方式禁用mapviews regionWillChange吗?

动画前:

之前

动画中途.看到很多错失的注释.

中间

动画完成了.所有错失的注释都消失了.

后

art*_*dev 2

作为临时解决方案,我建议您执行以下操作:
1. 在更改其框架之前,使用地图视图的屏幕截图创建 UIImageView
2. 将该 imageView 作为子视图添加到父视图并隐藏您的地图视图。
3. 通过更改其框架来对 imageView 和 mapView 进行动画处理。
4. 在动画完成块中,取消隐藏您的mapView 并删除imageView。

像这样的东西:

- (UIImage *)imageFromView:(UIView *) view
{
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [[UIScreen mainScreen] scale]);
    } else {
        UIGraphicsBeginImageContext(view.frame.size);
    }
    [view.layer renderInContext: UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (void)foo
{
    UIImageView *mapScreenShotView = [[UIImageView alloc] initWithImage:[self imageFromView:self.mapView]];
    mapScreenShotView.frame = self.mapView.frame;
    [self.mapView.superview addSubview:mapScreenShotView];
    self.mapView.hidden = YES;

    CGRect frame = self.mapView.frame;
    frame.origin = ...
    frame.size   = ...
    // here no more need to animate mapView, because currently its hidden. So we will change it's frame in outside of animation block.
    self.mapView.frame = frame;
    [UIView animateWithDuration:0.25f animations:^{
       mapScreenShotView.frame = frame;
    } completion:^{ 
       self.mapView.hidden = NO;
       [mapScreenShotView removeFromSuperview];
    }];
}
Run Code Online (Sandbox Code Playgroud)

使用这个临时解决方案,直到找到解决该问题的正确方法。