设置UIScrollView的内容大小的起点 [英] Set starting point of content size for UIScrollView

查看:215
本文介绍了设置UIScrollView的内容大小的起点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I set the starting point of a UIScrollView? I would like to add a UIImageView left of the UIScrollView but changing the contentSize only adds scrolling room to the right of the scrollview. How do I add an ImageView left of the scrollView's (0,0) point and make it part of the scrollview's content size?

推荐答案

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

Hopefully I've got what you're trying to do here. I think this just takes a few turns with the contentOffset to get right.

开始;


  • 在第0帧(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在滚动条内容的左侧和右侧。

  • Add the scrollView at frame (0,0,320,480) - its a full screen scroller
  • set contentSize to (320*3, 480) - it now has a content with the width of 3 'pages'
  • Add your imageView as a subview to the scrollView at frame (320,0,320,480)
  • set contentOffset of the scrollView to (320, 0) - this will move the content of the scrollView left, in the negative x direction by 320
  • Now your imageView will be on screen, but it will have a 320px width both on the left and right on the scroller content.

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

(Note that in the code below, i've simply added a UIView and not an 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];

这有帮助吗?

这篇关于设置UIScrollView的内容大小的起点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆