3

我想在滚动视图上显示我的数据库中存在的图像。此外,我想在一行中显示 4 个图像,然后在下一行显示下一个 4 个,依此类推。最初,滚动视图将仅显示 2 行,并且在用户垂直滚动后将能够滚动浏览数据库中存在的所有图像。任何人都可以提出任何合适的措施来执行此操作或为此提供任何示例演示或代码。任何帮助将不胜感激。

我正在使用此代码以水平方式获取图像。如何在连续 5 张图像后以垂直方式进行操作。我的代码:-

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

在视图中加载我创建滚动视图为: -

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];

谢谢,克里斯蒂

4

2 回答 2

2

你看过 Three20 的相册控件吗?有一个here关于如何使用它的教程(作者 Ray Wenderlich)。

于 2011-06-07T12:57:20.830 回答
1

- - - 更新 - - - -

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

-----更新完毕-----

给你,

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

上面的代码用于 iPad 设置图像,宽度和高度分别为 100 和 80。. cx 和 cy 变量用于跟踪定位下一个图像。在此处的 1 行中,6 个图像是可能的(横向模式)。根据您的要求修改代码。

如果您需要进一步的帮助,请发表评论。

希望这可以帮助。

于 2011-06-07T13:56:38.777 回答