开发者

Creating UIScroll view with rows of images

I want to display the images present in my database on the Scroll view.Also i want to display 4 images in a row then next 4 in next row and so on.Initially the scroll view will show only 2 rows and after scrolling vertically the user will be able to scroll through all the images present in the database.Can anyone suggest any suitable measures to do this or provide any sample demo or code for this.Any help will be appreciated.

I am using this code to get images in horizontal fashion .How can i do it in vertical fashion after 5 images in a row.My code:-

 - (void)layoutScrollImages
 {

     UIImageView *view = nil;
    NSArray *subviews = [scrollView1 subviews];
    CGFloat curXLoc = 40;
// reposition all image subviews in a horizontal serial fashion

//CGFloat curYLoc = 0;
for (view in subviews)
{
    //if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
    if(view)
    {
        CGRect frame = view.frame;
        frame.origin = CGPointMake(curXLoc, 40);
        view.frame = frame;
        curXLoc += (kScrollObjWidth);           

    }
}

     // set the content size so it can be scrollable
     [scrollView1 setContentSize:CGSizeMake((20 * kScrollObjWidth),[scrollView1 bounds].size.height)];
     //[self layoutScrollLabels];
 }

And in view did load i created scroll view as:-

scrollView1 = [[UIScrollView alloc] initWithFrame:CGRectMake(25,48,630,180)];
[scrollView1 setBackgroundColor:[UIColor lightGrayColor]];
[scrollView1 setCanCancelContentTouches:NO];
//scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
//scrollView1.clipsToBounds = YES;      // default is NO, we want to restrict drawing within our scrollview
scrollView1.scrollEnabled = YES;
scrollView1.pagingEnabled=YES;
scrollView1.showsVerticalScrollIndicator = YES;
//scrollView1.showsHorizontalScrollIndicator = YES;
//scrollView1.alwaysBounceVertical = YES;
//scrollView1.alwaysBounceHorizontal = NO;

    [self.view addSubview:scrollView1];

T开发者_Python百科hanks, Christy


Have you looked at Three20's photo gallery control? There is a tutorial here (By Ray Wenderlich) on how to use it.


------Updated-------

- (void)layoutScrollImages
{
    //Considered your Image width & height as 50

    int i; //Variable to keep count per line;
    CGFloat curXLoc = 10; //Variable to keep X Location track
    CGFloat curXLoc = 0; //Variable to keep Y Location track
    for (UIImageView *tmpImgView in [scrollView1 subviews])
    {
        if(tmpImgView)
        {
            CGRect frame = view.frame;
            frame.origin.x = curXLoc;
            frame.origin.y = curYLoc;
            view.frame = frame;

            curXLoc = curXLoc + 55; // Adding 55 for next photo X position

            if(i!=1 && i%5 == 0)
            {
                curXLoc = 10; //Reset X Location so that it will start from left again;
                curYLoc = curYLoc + 55; // Adding 55 for next photo Y position if 5 photos are placed in one row
            }
        }
        i++;
    }

    // set the content size so it can be scrollable
    [scrollView1 setContentSize:CGSizeMake(yourScrollViewWidth,curYLoc+100)];
}

-----Updated Finished-------

Here you are,

-(void)setImagesInScrollView
{
    scrollView.delegate = self;

    int i=1;
    CGFloat cx = 0;
    CGFloat cy = 0;

    for(UIImageView *yourImgView in yourImgArray)
    {
        //Create Image
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 80)];
        imgView.contentMode = UIViewContentModeCenter;
        imgView.image = yourImgView.image;

        CGRect rect = imgView.frame;
        rect.size.height = imgView.frame.size.height;
        rect.size.width = imgView.frame.size.width;
        rect.origin.x += cx;
        rect.origin.y += cy;
        imgView.frame = rect;

        cx+=imgView.frame.size.width + 60;
        [scrollView addSubview:imgView];

        if(i!=1 && i%6==0)
        {
            cx=62;
            cy+=155;
        }

        [imgView release];
        i++;
    }

    [scrollView setContentSize:CGSizeMake(1024, cy+150)];
    [scrollView setFrame:CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, 1024, 630)];
    [scrollView setContentSize:CGSizeMake(1024, cy+150)];
    [scrollView setContentOffset:CGPointMake(0.0, 0.0)];
}

Above code was for iPad setting Images with 100 & 80 width & height respectively. . cx and cy variables are used to keep track for positioning next image. In 1 line here 6 Images are possible (Landscape mode). Modify code for the same as per your requirement.

If you need further help please leave a comment.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜