简体   繁体   English

iPhone水平滚动

[英]iPhone Horizontal Scrolling

I am trying to create an app with horizontal scrolling, so that one would be able to scroll horizontally through a series of images. 我正在尝试使用水平滚动创建一个应用程序,以便可以在一系列图像中水平滚动。 I watched the WWDC Session 104 video on this, and while they made an interesting app, they flew through the basics of it very quickly. 我观看了WWDC Session 104视频,虽然他们制作了一个有趣的应用程序,但他们很快就完成了它的基础知识。

I understand using the UIScrollView, and that I have to enable paging. 我理解使用UIScrollView,我必须启用分页。 After that they say that I should add more views as subviews of the scrollview, but I am not clear on how to do that. 之后他们说我应该添加更多视图作为scrollview的子视图,但我不清楚如何做到这一点。 I am also not clear on how I add my images to those views. 我也不清楚如何将我的图像添加到这些视图中。

As you can probably tell I am pretty new at this so any help would be appreciated. 你可能会告诉我这是一个很新的所以任何帮助将不胜感激。

You want to look into UIImageView . 你想看看UIImageView It's a view specifically for holding images. 这是专门用于保存图像的视图。

When you add your images, you want to set their rects (probably using initWithFrame: for each UIImageView ) so that: 添加图像时,您需要设置它们的rects(可能使用initWithFrame:对于每个UIImageView ),以便:

  • the first image is at 0,0 第一张图片是0,0
  • the second image is at 320,0 第二张图像是320,0
  • third is at 640,0 (etc) 第三是640,0(等)

Ie each image is 320 pixels right of the previous. 即每个图像都是前一个320像素。

The final step is to set the contentSize for your UIScrollView -- this is a CGSize which describes the total size of the scroll view. 最后一步是为UIScrollView设置contentSize - 这是一个CGSize ,它描述了滚动视图的总大小。

If you have 3 images, you would then set it to (320*3) * 480 using eg 如果您有3张图像,则可以使用例如将其设置为(320 * 3)* 480

myScrollView.contentSize = CGSizeMake(320*3, 480);

A lot of people, when they initialize the scroll view, have a for loop or similar which steps through the images they want to display. 许多人在初始化滚动视图时,都有一个for循环或类似的循环,他们会逐步浏览他们想要显示的图像。 These for loops tend to look something like this: 这些for循环往往看起来像这样:

CGFloat scrollWidth = 0.f;
for (UIImage *someImage in someNSArrayWithImages) {
    UIImageView *theView = [[UIImageView alloc] initWithFrame:
        CGRectMake(scrollWidth, 0, 320.f, 480.f)];
    theView.image = someImage;
    [myScrollView addSubview:theView];
    [theView release];
    scrollWidth += 320.f;
}
myScrollView.contentSize = CGSizeMake(scrollWidth, 480.f);

This way you'll get things lined up and you'll get the content size for you at the same time. 通过这种方式,您可以排列内容并同时为您获取内容大小。

If you want to make it so that the scroll view "intelligently" scrolls to each image and stops when people slide left/right, you can do myScrollView.pagingEnabled = YES . 如果你想使滚动视图“智能地”滚动到每个图像并在人们向左/向右滑动时停止,你可以执行myScrollView.pagingEnabled = YES

Hope that helps get you going. 希望有助于您前进。

Assuming you have "infinite" images, putting them all there at or before launch time in a huge UIScrollView will not be an option. 假设你有“无限”的图像,那么在巨大的UIScrollView中,在发布时或之前将它们全部放在那里是不可取的。 (there is a limit to the size of a UIView) (UIView的大小有限制)

The way I solved it: Make a UIScrollView covering the whole screen. 我解决它的方式:制作覆盖整个屏幕的UIScrollView。 It's content should be a UIView of 3*320 width and 480 height, extending 320px left and 320px right. 它的内容应该是3 * 320宽度和480高度的UIView,向左延伸320px,向右延伸320px。

Put 3 UIImageView's in it, left, middle and right. 放入3个UIImageView,左,中,右。 Set paging=YES, so the uiscrollview clips to the 3 "pages" you've created. 设置paging = YES,因此uiscrollview剪辑到您创建的3“页面”。

Make sure your class is the delegate of the uiscrollview, and listen for 确保您的课程是uiscrollview的代表,并倾听

-(void)scrollViewDidEndDragging:(UIScrollView*)sv willDecelerate:(BOOL)notFinished
-(void)scrollViewDidEndDecelerating:(UIScrollView*)sv
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView*)sv

and make the appropriate transitions on hitting paging boundaries; 并且在击中寻呼边界时进行适当的转换; shift images and set ContentOffset so you're looking at the center image again. 移动图像并设置ContentOffset,这样您就可以再次查看中心图像。

I suggest you make this first, and only then read on... 我建议你先做这个,然后继续阅读......

Then you will hit on a bug, documented here UIScrollView - (bounces = NO) seems to override (pagingEnabled = YES) and here http://www.iphonedevsdk.com/forum/iphone-sdk-development/935-paging-uiscrollview.html , which makes that you cannot disable bouncing and have paging enabled at the same time. 然后你会遇到一个bug,这里记录的UIScrollView - (bounces = NO)似乎覆盖(pagingEnabled = YES)http://www.iphonedevsdk.com/forum/iphone-sdk-development/935-paging-uiscrollview .html ,这使得您无法禁用弹跳并同时启用分页。 So enable bouncing, and subclass UIScrollView, overruling setContentOffset in there to prevent bouncing. 因此,启用弹跳和子类UIScrollView,在那里覆盖setContentOffset以防止弹跳。 (having bouncing really enabled will make for a rather unusual user experience) (真正启用弹跳将带来相当不寻常的用户体验)

Have a look at Apple's PageControl sample code . 看看Apple的PageControl示例代码 It's fairly short and easy to follow so you'll get the gist of setting up a project where multiple view controllers are loaded as you swipe horizontally. 它相当简短,易于理解,因此您将获得设置项目的要点,在水平滑动时加载多个视图控制器。

Once you have this setup then it's the view controller's responsibility to load its own content (in your case, an image). 一旦你有了这个设置,视图控制器就有责任加载自己的内容(在你的情况下,是一个图像)。 You should make sure you understand how to load images first (using threads, etc) before you tackle paging, etc. 在解决分页等问题之前,您应该确保了解如何首先加载图像(使用线程等)。

Think of it as two independent tasks. 把它想象成两个独立的任务。 The view control is responsible for loading and displaying an image. 视图控件负责加载和显示图像。 The scroll view with paging just tells the appropriate view controller when to load itself (it doesn't care what the view controller does once its loaded) 带分页的滚动视图只是告诉适当的视图控制器何时加载自身(它不关心视图控制器加载后的作用)

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM