Question

I'm building an app based upon the "Scrolling" sample code that Apple provided. All is working very well. The nature of the images that I want to display, would make it desirable, if the order of the images is reversed, and that the first visible image is the right-most, rather than the left most. Basically, the user should scroll back, from right to left, rather than from left to right. But now: I don't understand the syntax Apple is using, and I hope someone can explain to me what is going on. Here are the relevant parts of the sample app:

- (void)viewDidLoad
{
    self.view.backgroundColor = [UIColor whiteColor];

    // load all the images from our bundle and add them to the scroll view
    NSUInteger i;
    for (i = 1; i <= kNumImages; i++)
    {
        NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", i];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
        CGRect rect = imageView.frame;
        rect.size.height = kScrollObjHeight;
        rect.size.width = kScrollObjWidth;
        imageView.frame = rect;
        imageView.tag = i;  // tag our images for later use when we place them in serial fashion
        [scrollView1 addSubview:imageView];
        [imageView release];
    }

    [self layoutScrollImages];  // now place the photos in serial layout within the scrollview

}

- (void)layoutScrollImages
{
    UIImageView *view = nil;
    NSArray *subviews = [scrollView1 subviews];

    // reposition all image subviews in a horizontal serial fashion
    CGFloat curXLoc = 0;
    for (view in subviews)
    {
        if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
        {
            CGRect frame = view.frame;
            frame.origin = CGPointMake(curXLoc, 0);
            view.frame = frame;

            curXLoc += (kScrollObjWidth);
        }
    }

    // set the content size so it can be scrollable
    [scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
Was it helpful?

Solution

So this is what I ended up doing: I first inverted the order of the subviews, and then I made the scrollview jump to the last 'frame', by adding the following lines:

CGPoint lastFrame = CGPointMake(((kNumImages -1) * kScrollObjWidth), 0.0f);
[scrollview setContentOffset:lastFrame];

I hope that this somehow is useful to somebody...

OTHER TIPS

It looks like you need to modify layoutScrollImages. Initialize curXLoc to the max number needed, and decrement it in the loop.

- (void)layoutScrollImages
{
    UIImageView *view = nil;
    NSArray *subviews = [scrollView1 subviews];

    // reposition all image subviews in a horizontal serial fashion
    CGFloat curXLoc = kNumImages * kScrollObjWidth;
    for (view in subviews)
    {
        if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
        {
                CGRect frame = view.frame;
                frame.origin = CGPointMake(curXLoc, 0);
                view.frame = frame;

                curXLoc -= (kScrollObjWidth);
        }
    }

    // set the content size so it can be scrollable
    [scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top