4

我有一个滚动视图,它会自动生成一系列包含图像视图的子视图。这个概念是它几乎是一个自定义的 PDF 样式阅读器,其中每个页面都作为图像加载到 imageview 中。共有三个“层”——外部 UIScrollView、自动生成的 subViews 和 subview 内部的 imageview。

我在这方面遇到了一些麻烦,我之前问过这个问题,但不幸的是,我的问题的核心是在错误的地方。这是我的第二次尝试:

旋转时,一切都根据需要旋转。不幸的是,这就是我得到的: 替代文字

显然,我希望图像 1 居中,并且在您轻拂视图之前没有图像 2 的提示。

这是我设置视图的代码:

- (void)loadView {
    [self setUpView];
}
- (void)setUpView {
    //INITIALISE PAGING SCROLL VIEW
    CGRect pagingScrollViewFrame = [[UIScreen mainScreen] bounds];
    pagingScrollViewFrame.origin.x -= 10;
    pagingScrollViewFrame.size.width += 20;
    pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
    pagingScrollView.contentMode =  UIViewContentModeScaleAspectFit;
    
    //CONFIGURE PAGING SCROLL VIEW  
    pagingScrollView.pagingEnabled = YES;
    pagingScrollView.backgroundColor = [UIColor blackColor];
    pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width*7, pagingScrollViewFrame.size.height);
    
    //ACTIVATE PAGING SCROLL VIEW
    self.view = pagingScrollView;
    
    //ADD PAGES TO SCROLL VIEW
    for (int i = 0; i < 7; i++){
        ImageScrollView *page = [[[ImageScrollView alloc] init] autorelease];
        [self configurePage:page forIndex:i];
        [pagingScrollView addSubview:page];
    }
}

如何重新定义框架的大小?我应该调用什么函数等。如何使图像居中?

我是新手,所以我为我的问题模棱两可道歉。

干杯

4

2 回答 2

3

我已经想通了。我需要更改的是各个页面的框架,因此(在另一个问题的帮助下)我编写了一个重新定向方法。

第一步是因为我发现我需要遍历每个页面并重新调整每个框架的大小,然后将框架重新应用于视图。为此,我需要创建一个数组,将其填充到上面的 For 循环中。我总共有 7 页。

然后我可以(在旋转时)调用下面的 re-orient 方法来重新调整每个视图的大小:

- (void) reOrient{
    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){    
        CGRect f;
        int i = 0;
        for (ImageScrollView *page in pageArray) {          
            f = page.frame;
            f.size.width = 320;
            f.origin.x = (f.size.width * i);
            page.frame = f;
            pagingScrollView.contentSize = CGSizeMake(f.size.width * 7, 480);
            i++;
        }
    }else{
        CGRect f;
        int i = 0;
        for (ImageScrollView *page in pageArray) {          
            f = page.frame;
            f.size.width = 480;
            f.origin.x = (f.size.width * i);
            page.frame = f;
            pagingScrollView.contentSize = CGSizeMake(f.size.width * 7,  480);
            i++;
        }
    }
}
于 2010-10-19T16:50:32.007 回答
1

这对我有用:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    //get current page based on offset before rotation    
    NSInteger currentPage = self.scrollView.contentOffset.x / self.scrollView.bounds.size.width;

    //set anticipated scrollview content size by switching width and height (they will be switched after rotation)
    [self.scrollView setContentSize:CGSizeMake(totalPages * self.view.bounds.size.height, self.view.bounds.size.width)];

    //set the anticipated content offset based on height before rotation
    [self.scrollView setContentOffset:CGPointMake(currentPage * self.scrollView.bounds.size.height, 0.0f)];
}

基本上我在旋转之前为新的滚动视图内容大小设置内容偏移量(我正在为预期的高度和宽度切换高度和宽度)。

旋转全屏分页 UIScrollView 时,这对我有用。

所有分页子视图都有以下自动调整大小的掩码,以便它们在旋转后整齐地落入到位:

UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin
于 2014-01-27T13:17:21.570 回答