views:

574

answers:

1

Hi Folks,

Got this code for a viewscroller from the apple developers site.

@synthesize scrollView1, scrollView2;

const CGFloat kScrollObjHeight  = 467.0;
const CGFloat kScrollObjWidth   = 320.0;
const NSUInteger kNumImages     = 6;


- (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)];
}

it works brilliantly the images are rotated by pulling them left or right. but I want to pull the images up and down to change them.

Can anyone help me out with this? this part in the code looks as if it controls the direction but i'm not sure how to change it.

// 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);
        }
    }

Any help most appreciated, thanks!

Billy

+2  A: 

To change scrolling "direction" from horizontal to vertical you basically should swap the way you process x and y coordinates. The code will look like:

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

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

            curYLoc += (kScrollObjHeight);
        }
    }

    // set the content size so it can be scrollable
    [scrollView1 setContentSize:CGSizeMake([scrollView1 bounds].size.width, kNumImages * kScrollObjHeight)];
}
Vladimir
Hi Vladimir, thanks for your response. I've tried your code but am getting 3 errors:curXLoc undeclared,Too few arguments to function 'CGSizeMake', andToo few arguments to 'setContentSize'Can you help me out?Billy
iamjonesy
Sorry made some typos - curXLoc must be curYLoc and also should remove extra parentheses in code
Vladimir
brilliant that works thanks! one other question though if you dont mind :) the scrollview now at the top and you slide ur fingers up to move to the next view. How can i change that so when i start i have to scroll down instead?Billy
iamjonesy
It seems you need to calculate and set appropriate contentOffset (using -setContentOffset method)
Vladimir
cheers! what i've done is NSUInteger ScrollWidth = kNumImages * 390; (dunno why 390 but it just works) then [scrollView1 setContentOffset:CGPointMake(0,ScrollWidth) animated:YES];
iamjonesy