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 に答える