SamRowley
SamRowley

Reputation: 3485

pagingScrollView Slideshow

I have started trying to understand the way apple implements the photoviewer app for the iPhone after watching both videos from WWDC 2009 and 2010 about scrollViews and paging through photographs and I am taking each step very slowly to understand the processes very well before I implement it all into my app.

I have run into a couple of problems at the moment:

  1. Firstly I have it set up to have a paging scroll view to swipe left and right between photos with a bit of space in between like on the videos but when I add an image inside a UIImageView th image is too big for the screen, I have tried adding UIViewContentModeScaleAspectFit; to no effect.

  2. When I go through the for loop to look at each element of the array containing the images the UIViews overlap and show one photograph on top of the other, I would like to know how to separate them into the next section of the paging scroll view.


- (void)loadView{
    UIImage *img0 = [UIImage imageNamed:@"29.png"];
    UIImage *img1 = [UIImage imageNamed:@"33.png"];

NSMutableArray *imgArray = [[NSMutableArray alloc] initWithObjects:img0, img2, nil]; CGRect pagingScrollViewFrame = [[UIScreen mainScreen] bounds]; pagingScrollViewFrame.origin.x -= 10; pagingScrollViewFrame.size.width += 20; pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame]; pagingScrollView.pagingEnabled = YES; pagingScrollView.backgroundColor = [UIColor blackColor]; pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width * [imgArray count], pagingScrollViewFrame.size.height); self.view = pagingScrollView; for (int i=0; i < [imgArray count]; i++) { UIImageView *page = [[UIImageView alloc] initWithImage: [imgArray objectAtIndex:i]]; page.contentMode = UIViewContentModeScaleAspectFit; [pagingScrollView addSubview:page]; }}

As I have mentioned I am fairly new to programming for the iPhone and am taking things slowly to fully understand, eventually this program will mimic the native app with pinching and tapping for zoom.

Upvotes: 0

Views: 502

Answers (2)

Matt Long
Matt Long

Reputation: 24476

If you're using UIImageViews, you should make sure that your view has its clipsToBounds field set to yes. Try adding:

UIImageView *page = [[UIImageView alloc] 
                           initWithImage:[imgArray objectAtIndex:i]];
[page setContentMode:UIViewContentModeScaleAspectFit];
[page setClipsToBounds:YES];
[pagingScrollView addSubview:page];

Then, make sure you are setting your image's frame to the correct offset within the scroll view. The frame's origin.x needs to be the width of the frame times the image index. So you need something like this:

[page setFrame:CGRectMake(i*pagingScrollViewFrame.size.width, y, width, height)];

where i is your index from your for loop.

Though in the sample code from the WWDC session you're referring to, this is done in a method called -configurePage. Have you downloaded the sample code?

Upvotes: 3

tc.
tc.

Reputation: 33592

-[UIImageView initWithImage:] sets the frame to be the image's size. You'll need to use page.frame = [[UIScreen mainScreen] bounds] or similar to scale images to the correct size.

Upvotes: 1

Related Questions