防止在设备轮换时调用MKMapViewDelegate方法

时间:2013-06-28 15:31:00

标签: ios objective-c ios6 mkmapview mkmapviewdelegate

我在

中添加了一些功能
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated

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

我希望在更改地图区域时调用它。

如何在设备更改其方向时阻止调用这些委托方法?

2 个答案:

答案 0 :(得分:-1)

添加属性BOOL didRotate

然后添加类似

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

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

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

答案 1 :(得分:-1)

当地图视图的框架发生更改时,将调用这些委托方法。由于自动布局约束或自动调整遮罩,框架可以在旋转时更改。因此,一种解决方案是保持地图视图框架不变。

您还可以尝试在界面方向更改时取消设置委托。

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


-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    _mapView.delegate = self;    
}