2番目のVCのアプリではUICollectionViewCell
、いくつかの画像のミニチュアがあり、タップすると、セル内にあるすべての画像でフルスクリーンにセグエされます(たとえば、セルに3つの画像がある場合(スクロールビューが内側にある)、その3つの画像でフルスクリーンプレゼンテーションにセグエ) . いくつかの画像を表示するために、1 つの scrollView を別の内部で使用しています (解決策は、StackOverflow のどこかにあります)。
ストーリーボードには、メインの 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) は、内部 scrollView に画像を入力し、フレームを設定するためのメソッドです。フレームは向きによって異なります
- (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 ですべてのビューとサブビューのフレームを設定しようとしましたが、その後、スクロールビューの動作が奇妙になります。
2 つのスクロールビュー (1 つが別のもの) を使用して画像を正しく表示するときに、回転を適切に処理するにはどうすればよいですか?