如何配置UIScrollView以使可见区域永远不会超出背景图像?

时间:2012-09-01 03:55:11

标签: objective-c ios cocoa-touch uiscrollview

在花了半个晚上试图让我自己开始工作之后我终于转向了StackWisdom:

我正在尝试使用一种视差滚动背景图像(类似于Windows Phone 7上的UI)实现水平的分页UIScrollView。我已经设法使用scrollViewDidScroll让滚动工作正常。对于3页的例子,我使用的是1460x480 / 2920x960像素的图像(3 * 320点+ 2 * 250填充宽度)​​。根据我在scrollViewDidScroll中使用的因素,滚动视图的右边缘也可以正常工作。

然而,当在第1页上并且尝试向左滚动时,可以看到背景图像在屏幕的边界处结束,并且窗口(灰色,白色,无论如何)背景变得可见。如何配置滚动​​视图,以便在内容区域左侧有一个额外的250像素的背景图像?我尝试了插图,偏移,重新定位内容区域和无数组合,但无济于事。

另外,早上5点30分,我很累。 -_-

来源:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    CGRect screenRect = [[self window] bounds];

    // Create and configure scroll view
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
    [scrollView setDelegate:self];
    [scrollView setPagingEnabled:YES];
    [scrollView setShowsHorizontalScrollIndicator:NO];

    [[self window] addSubview:scrollView];

    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(50, 50, 200, 50)];
    [pageControl setNumberOfPages:3];
    [pageControl setCurrentPage:0];
    [pageControl setBackgroundColor:[UIColor clearColor]];
    [[self window] addSubview:pageControl];

    backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"desert_panorama_cropped.png"]];
    [scrollView addSubview:backgroundImageView];

    // Create first content view
    screenRect.origin.x = 0;
    ContentView *firstContentView = [[ContentView alloc] initWithFrame:screenRect string:@"Page 1"];
    [scrollView addSubview:firstContentView];

    // Create second content view
    screenRect.origin.x = screenRect.size.width;
    ContentView *secondContentView = [[ContentView alloc] initWithFrame:screenRect string:@"Page 2"];
    [scrollView addSubview:secondContentView];

    // Create third content view
    screenRect.origin.x = screenRect.size.width * 2.0;
    ContentView *thirdContentView = [[ContentView alloc] initWithFrame:screenRect string:@"Page 3"];
    [scrollView addSubview:thirdContentView];

    CGRect contentRect = screenRect;
    contentRect.size.width *= 3.0;
    [scrollView setContentSize:contentRect.size];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    float x = scrollView.contentOffset.x;
    NSLog(@"Scrollview did scroll to offset: %f", x);

    CGRect backgroundImageFrame = backgroundImageView.frame;
    backgroundImageFrame.origin.x = x/1.5;
    backgroundImageView.frame = backgroundImageFrame;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int newOffset = scrollView.contentOffset.x;
    int newPage = (int)((newOffset)/(scrollView.frame.size.width));
    [pageControl setCurrentPage:newPage];
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我认为你要做的就是像这样在这一行添加一个常量:

backgroundImageFrame.origin.x = x/1.5 - 250;