Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable adding annotations to mapView while animating its frame

I am animating the frame of my mapView with a UIView animation:

     [UIView animateWithDuration:0.25f animations:^{
           self.mapView.frame = frame;
        } completion:nil];
}

While expanding the mapView with the above I see that mapView is adding misplaced annotations until the animation is over. When the animation is finished the misplaced annotations disappears.

The calls are as follows when I expand the map:

  1. animateWithDuration begin
  2. mapView:regionWillChangeAnimated
  3. mapView:didAddAnnotationViews
  4. mapView:regionDidChangeAnimated
  5. animateWithDuration complete

No problem when I minimise the map probably because the mapView:didAddAnnotationViews isn't called.

Can I somehow disable mapviews regionWillChange while animating?

Before animation:

before

Halfway in the animation. Seeing a lot of missplaced annotations.

middle

Animation is done. All missplaced annotations is gone.

after

like image 396
Toydor Avatar asked Oct 20 '22 06:10

Toydor


1 Answers

As a temporary solution I can suggest you to do following:
1. Make a UIImageView with screenShot of your mapView before changing it's frame
2. Add that imageView as subView to your parent view and hide your mapView.
3. animate both: imageView and mapView with changing it's frames.
4. In completion block of animation unhide your mapView and remove imageView.

Something like this:

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

Use this temporary solution until you find a right way to solve that problem.

like image 82
arturdev Avatar answered Oct 24 '22 10:10

arturdev