2

I am adding some functionality in

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated

and

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

I want this to be called when the map region is changed.

How can i prevent these delegate methods from being called when device changes its orientation?

David
  • 34,223
  • 3
  • 62
  • 80
Lollypop
  • 251
  • 1
  • 5
  • 14
  • 1
    I wouldn't try preventing the delegate methods from being called, but rather, I'd be inclined to add logic to my delegate method that only performed the necessary actions on certain conditions. For example, keep track of the previous `center` of the map view, and if it's the same the next time your delegate method is called, then perhaps don't perform the task. Or just save the "previous" orientation and compare that to the current orientation. Lots of approaches. – Rob Jun 28 '13 at 16:52
  • Can anyone give assured answer for this post? I want answer for this post too!!! – Mohd Sadham Nov 27 '14 at 13:03

2 Answers2

-1

Add a property BOOL didRotate

Then add something like

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    self.didRotate = YES;
}

and

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{
    if(self.didRotate) return;
}

and

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    if(self.didRotate) {
        self.didRotate = NO;
        return;
    }
}
  • we cannot assure that willRotateToInterfaceOrientation will be called before regionWillChangeAnimated. In my case regionWillChangeAnimated and regionDidChangeAnimated is called first. – Lollypop Jun 28 '13 at 15:56
-1

These delegate methods are called when the frame of the map view changes. The frame can change on rotation because of autolayout constraints or autoresizing masks. So one solution would be to keep the map view frame constant.

You could also try to unset the delegate while the interface orientation is changing.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    _mapView.delegate = nil;
}


-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    _mapView.delegate = self;    
}
Felix
  • 35,354
  • 13
  • 96
  • 143
  • we cannot assure that willRotateToInterfaceOrientation will be called before regionWillChangeAnimated. In my case regionWillChangeAnimated and regionDidChangeAnimated is called first, so the mapview delegate will not be nil at that time. – Lollypop Jun 28 '13 at 16:10