设置UIScrollView的内容大小的起点

gur*_*ooj 11 iphone objective-c uiscrollview ios4 contentsize

如何设置UIScrollView的起点?我想在UIScrollView左边添加一个UIImageView,但更改contentSize只会在scrollview右侧添加滚动空间.如何在scrollView的(0,0)点左侧添加ImageView并使其成为scrollview内容大小的一部分?

Mad*_*dhu 9

希望我有你想要做的事情.我认为这只需要几轮就可以使用contentOffset来实现.

开始;

  • 在frame(0,0,320,480)处添加scrollView - 它是一个全屏幕滚动条
  • 将contentSize设置为(320*3,480) - 它现在有一个宽度为3'页面'的内容
  • 将您的imageView作为子视图添加到scrollView at frame(320,0,320,480)
  • 将scrollView的contentOffset设置为(320,0) - 这将向左移动scrollView的内容,在负x方向上移动320
  • 现在你的imageView将在屏幕上,但在卷轴内容的左右两侧都有320px的宽度.

(请注意,在下面的代码中,我只是添加了一个UIView而不是一个imageView)

UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroller.delegate = self;
scroller.pagingEnabled = YES;
scroller.backgroundColor = [UIColor blueColor];
scroller.contentSize = CGSizeMake(960, 480);

UIView *imgView = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 480)];
[imgView setBackgroundColor:[UIColor redColor]];
[scroller addSubview:imgView];

[scroller setContentOffset:CGPointMake(320, 0)];
[self.view addSubview:scroller];
Run Code Online (Sandbox Code Playgroud)

这有帮助吗?