旋转后UIScrollViews帧

时间:2013-10-22 11:08:22

标签: ios objective-c uiscrollview

在第二个VC中的应用程序中我有UICollectionViewCell几个图像微缩模型,点击后你会看到单元格内的所有图像全屏(例如,如果单元格中有3个图像(滚动视图在里面),你就会离开用这三个图像到全屏演示)。 为了显示一些图像,我在另一个图像中使用了一个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];
        }];

不幸的是,这个解决方案的缺点是旋转后scrollview滚动到第一个元素并且我以编程方式滚动到scrollview中的前一个(当前)位置。 对于不想要的用户来说,这种变化是显而易见的。

我还尝试在willRotate中设置所有视图和子视图的帧,但之后滚动视图行为很奇怪。

如何在使用两个滚动视图(一个在另一个内部)并正确显示图像时正确处理旋转?

1 个答案:

答案 0 :(得分:0)

“我还尝试在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++;
    }