0

看起来很简单的任务。但是当我尝试使用setFrame方法调整大小时,我遇到了故障。还有一些其他 UIView 使用setFrame方法调整大小,并且效果很好。我用滑块和地图视图制作了自定义应用程序。SlideBar 更改 MKMapView 的 X 位置,但保持宽度等于屏幕宽度。这种方法适用于所有视图。但是 MKMapView 调整大小时遇到​​了麻烦。任何人都可以提供线索为什么会发生以及如何解决它?

4

1 回答 1

0

我遇到了同样的问题,这似乎只发生在 iOS 6(Apple Maps)上而不是 iOS 5(Google Maps)上。

我的解决方案是在用户开始拖动分隔手柄时拍摄地图的“屏幕截图”,在拖动过程中用此屏幕截图替换地图,并在松开手指时将地图放回原处。

对于 UIView 屏幕截图,我使用了How to capture UIView to UIImage 中的代码而不损失视网膜显示的质量,以及 Nikolai Ruhe 在How do I release a CGImageRef in iOS for a nice background color 中的回答。

我的 UIPanGestureRecognizer 操作是这样的(这(MKMapView)self.map(UIView)self.mapContainer在 Interface Builder 上设置自动调整大小的子视图):

- (IBAction)handleMapPullup:(UIPanGestureRecognizer *)sender
{
    CGPoint translation = [sender translationInView:self.mapContainer];

    // save current map center
    static CLLocationCoordinate2D centerCoordinate;

    switch (sender.state) {
        case UIGestureRecognizerStateBegan: {
            // Save map center coordinate
            centerCoordinate = self.map.centerCoordinate;

            // Take a "screenshot" of the map and set the size adjustments
            UIImage *mapScreenshot = [UIImage imageWithView:self.map];
            self.mapImage = [[UIImageView alloc] initWithImage:mapScreenshot];
            self.mapImage.autoresizingMask = self.map.autoresizingMask;
            self.mapImage.contentMode = UIViewContentModeCenter;
            self.mapImage.clipsToBounds = YES;
            self.mapImage.backgroundColor = [mapScreenshot mergedColor];

            // Replace the map with a screenshot
            [self.map removeFromSuperview];
            [self.mapContainer insertSubview:self.mapImage atIndex:0];
        } break;

        case UIGestureRecognizerStateChanged:
            break;

        default:
            // Resize the map to the new dimension
            self.map.frame = self.mapImage.frame;

            // Replace the screenshot with the resized map
            [self.mapImage removeFromSuperview];
            [self.mapContainer insertSubview:self.map atIndex:0];

            // Empty screenshot memory
            self.mapImage = nil;
            break;
    }

    // resize map container according do the translation value
    CGRect mapFrame = self.mapContainer.frame;
    mapFrame.size.height += translation.y;

    // reset translation to make a relative read on next event
    [sender setTranslation:CGPointZero inView:self.mapContainer];

    self.mapContainer.frame = mapFrame;
    self.map.centerCoordinate = centerCoordinate; // keep center
}
于 2013-08-05T12:00:48.973 回答