1

在花了半个晚上试图让我自己工作之后,我终于转向 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];
}

任何帮助将非常感激。

4

1 回答 1

0

我认为您所要做的就是在这一行中添加一个常量,如下所示:

backgroundImageFrame.origin.x = x/1.5 - 250;
于 2012-09-01T04:16:16.253 回答