开发者

iphone app scroll view is horizontal but i want it vertical - how do i change this?

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜