在第二个VC的应用程序中,我UICollectionViewCell
了很少的图像缩影,点击后,您可以进入全屏,其中包含单元格内的所有图像(例如,如果单元格中有3张图像(滚动视图在里面),您将这三张图像设置为全屏演示)。为了显示一些图像,我在另一个滚动视图中使用一个滚动视图(解决方案位于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)是用图片填充内部滚动视图和设置框架的方法,框架取决于方向
- (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);
}
要处理将旋转到...我正在删除旧的子视图并添加新的肖像框架。
-(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中设置所有视图和子视图的框架,但之后滚动视图行为很奇怪。
使用两个滚动视图(一个在另一个内部)时如何正确处理旋转并正确显示图像?
"我还尝试在willRotate中设置所有视图和子视图的框架,但之后滚动视图的行为很奇怪。
因此,这是解决问题的正确解决方案。我用这段代码做了,例如,在旋转到横向之前为所有子视图设置适当的框架。
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++;
}