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.
I understand using the UIScrollView, and that I have to enable paging. 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. 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. It’s a view specifically for holding images.
When you add your images, you want to set their rects (probably using
initWithFrame:for eachUIImageView) so that:I.e. each image is 320 pixels right of the previous.
The final step is to set the
contentSizefor yourUIScrollView— this is aCGSizewhich describes the total size of the scroll view.If you have 3 images, you would then set it to (320*3) * 480 using e.g.
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. These for loops tend to look something like this:
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.Hope that helps get you going.