2013-02-28 172 views
0

看起來像簡單的任務。但是,當我嘗試調整使用setFrame方法時,我得到了小故障。還有一些其他UIViews使用setFrame方法調整大小,它的工作原理非常完美。我使用滑塊和地圖視圖製作了自定義應用程序。 SlideBar更改MKMapView的X位置,但保持寬度等於屏幕寬度。這種方法適用於所有視圖。但MKMapView調整順利麻煩。任何人都可以請給出一個線索爲什麼發生這種情況以及如何解決它?調整大小MKMapView

回答

0

我有同樣的問題,這似乎只發生在iOS 6(蘋果地圖),而不是在iOS 5(谷歌地圖)。

我的解決辦法是採取一個地圖上的「截圖」,當用戶開始拖動分隔手柄,拖動期間用這張截圖的地圖,並把地圖背手指被釋放時。

我使用How to capture UIView to UIImage without loss of quality on retina display的代碼爲UIView屏幕截圖和Nikolai Ruhe的答案在How do I release a CGImageRef in iOS爲一個很好的背景顏色。

我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 
}