Cocoa touch 使用图像行创建UIScroll视图

Cocoa touch 使用图像行创建UIScroll视图,cocoa-touch,ipad,uiscrollview,horizontal-scrolling,vertical-scrolling,Cocoa Touch,Ipad,Uiscrollview,Horizontal Scrolling,Vertical Scrolling,我想在滚动视图上显示数据库中的图像。我还想在一行中显示4个图像,然后在下一行中显示下4个图像,依此类推。最初滚动视图将只显示2行,垂直滚动后,用户将能够滚动数据库中的所有图像。有人能建议任何合适的措施吗要做到这一点,或提供任何样本演示或代码。任何帮助将不胜感激 我正在使用此代码以水平方式获取图像。如何在连续5个图像之后以垂直方式获取图像。我的代码:- - (void)layoutScrollImages { UIImageView *view = nil; NSArray

我想在滚动视图上显示数据库中的图像。我还想在一行中显示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];
谢谢,
克里斯蒂

你看过Three20的照片库控件了吗?有一个关于如何使用它的教程(由Ray Wenderlich编写)。

----更新---

- (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)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)];
}
以上代码分别用于宽度和高度分别为100和80的iPad设置图像。cx和cy变量用于跟踪定位下一个图像。在这里的1行中,6个图像是可能的(横向模式)。根据您的要求修改相同的代码

如果您需要进一步帮助,请留下评论


希望这能有所帮助。

老兄,我的应用程序无法使用三个20项目类,请建议其他方法。在这里,我只能获取阵列中的最后一个图像,请帮助我,我无法找到它…此外,显示的图像在屏幕上没有正确显示。如果您有此问题,请提供任何其他示例代码properly@Christina这段代码适用于我,它来自我的Live项目。你面临什么问题?如果可能,请编辑您的问题并粘贴新代码,以便我们检查问题。我编辑了我的问题请检查并回答,我正在使用我的方法之一在滚动视图中添加图像。请帮助