2013-10-22 62 views
1

在第二個VC中的應用程序中,我有UICollectionViewCell與幾個圖像微縮模型,然後點擊後,您可以繼續全屏與細胞內的所有圖像(例如,如果有3個圖像在單元格中(scrollview在裏面)與三個圖像全屏演示)。 爲了顯示一些圖像,我在另一個裏面使用了一個scrollView(在這裏找到的解決方案,在StackOverflow上)。UIScrollViews旋轉後的框架

故事板上我只有主UIView。我在代碼中設置的所有其他內容。我知道,在使用自動佈局時,我不應該在代碼中使用框架,但使用帶旋轉的縮放的滾動視圖幾乎不可能設置適當的約束。我只在fullScreenVC中使用這個解決方案。

所以,在viewDidLoad

self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)]; 
self.outerScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)]; 

self.outerScrollView.showsHorizontalScrollIndicator = NO; 
self.outerScrollView.showsVerticalScrollIndicator = YES; 
self.outerScrollView.delegate = self; 
self.outerScrollView.pagingEnabled = YES; 
[self scrollViewDataForPortrait:NO]; 

[self.view insertSubview:self.outerScrollView belowSubview:self.blackInfoBox]; 
[self scrollToCurrentElement:[self.currentPictureToDisplay integerValue]]; 

scrollViewDataForPortatit:(BOOL)是用於填充有圖片內滾動視圖和用於設置幀的方法,幀取決於取向

- (void)scrollViewDataForPortrait:(BOOL)isPortrait 
{ 
    for (int gadabout = 0; gadabout < [self.imagesToDisplay count]; gadabout++) 
    { 
     CGRect frame; 
     frame.origin.x = self.imageView.frame.size.width * gadabout; 
     frame.origin.y = 0; 
     frame.size = self.imageView.frame.size; 

     UIScrollView *innerScrollView = [[UIScrollView alloc] initWithFrame:frame]; 

     UIImageView *newView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)]; 
     if (isPortrait) 
     { 
      [newView setFrame:CGRectMake(0, 0, 768, 1024)]; 
     } 
     newView.contentMode = UIViewContentModeScaleAspectFit; 
     newView.userInteractionEnabled = YES; 

     UIImage *painting = [self.imagesToDisplay objectAtIndex:gadabout]; 
     if (painting) 
     { 
      [newView setImage:painting]; 
     } 

     innerScrollView.minimumZoomScale = 1.0f; 
     innerScrollView.maximumZoomScale = 4.0f; 
     innerScrollView.delegate = self; 
     innerScrollView.tag = gadabout; 

     [self.arrayForUIImageViewsForZooming addObject:newView]; 
     [innerScrollView addSubview:newView]; 
     [innerScrollView setBackgroundColor:[UIColor blackColor]]; 

     [innerScrollView setContentSize:CGSizeMake(1024, 768)]; 
     if (isPortrait) 
     { 
      [innerScrollView setContentSize:CGSizeMake(768, 1024)]; 
     } 
     [self.outerScrollView addSubview:innerScrollView]; 
    } 
    self.outerScrollView.contentSize = CGSizeMake(self.outerScrollView.frame.size.width * self.imagesToDisplay.count, self.outerScrollView.frame.size.height); 
} 

要willRotateTo處理旋轉。 ..我刪除舊的子視圖並添加新的肖像幀。

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    self.outerScrollView.zoomScale = 1.0; 

    if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) 
    { 
     [UIView animateWithDuration:0.3f animations:^{ 
      [self.outerScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
      [self.imageView setFrame:CGRectMake(0, 0, 1024, 768)]; 
      [self.outerScrollView setFrame:CGRectMake(0, 0, 1024, 768)]; 
      [self.arrayForUIImageViewsForZooming removeAllObjects]; 
      [self scrollViewDataForPortrait:NO]; 
     } completion:^(BOOL finished) { 
      [UIView animateWithDuration:0.5f animations:^{ 
       [self scrollToCurrentElement:[self.currentPictureToDisplay integerValue]]; 
      } completion:nil]; 
     }]; 
    } 
    else 
    { 
     [UIView animateWithDuration:0.3f animations:^{ 
      [self.outerScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
      [self.imageView setFrame:CGRectMake(0, 0, 768, 1024)]; 
      [self.outerScrollView setFrame:CGRectMake(0, 0, 768, 1024)]; 
      [self.arrayForUIImageViewsForZooming removeAllObjects]; 
      [self scrollViewDataForPortrait:YES]; 
     } completion:^(BOOL finished) { 
      [UIView animateWithDuration:0.3f animations:^{ 
       [self scrollToCurrentElement:[self.currentPictureToDisplay integerValue]]; 
      } completion:nil]; 
     }]; 

不幸的是這種解決方案的缺點是事實,後旋轉滾動視圖滾動到第一個元素,我編程方式滾動,滾動型先前(當前)的位置。 這種變化對於不想要的用戶來說是顯而易見的。

我也嘗試在willRotate中設置所有視圖和子視圖的幀,但之後scrollviews行爲很奇怪。

如何在使用兩個滾動視圖(一個在另一個滾動視圖)並正確顯示圖像時正確處理旋轉?

回答

0

「我還嘗試在willRotate中設置所有視圖和子視圖的幀,但之後的scrollviews行爲很奇怪。」

所以這是解決問題的適當解決方案。 我用這段代碼完成了,例如在旋轉到橫向之前爲所有子視圖設置適當的框架。

int gadabout = 0; 
    for (UIScrollView *innerScrollView in self.outerScrollView.subviews) 
    { 
     [innerScrollView setFrame:CGRectMake(1024 * gadabout, 0, 1024, 768)];  
     if ([innerScrollView isKindOfClass:[UIScrollView class]]) 
     { 
      [innerScrollView setContentSize:CGSizeMake(1024, 768)]; 
     }  
     for (UIImageView *innerImageView in innerScrollView.subviews) 
     { 
      [innerImageView setFrame:CGRectMake(0, 0, 1024, 768)]; 
     } 
     gadabout++; 
    }