【问题标题】:Creating UIScroll view with rows of images使用多行图像创建 UI 滚动视图
【发布时间】:2011-09-10 01:16:06
【问题描述】:

我想在滚动视图上显示我的数据库中存在的图像。此外,我想在一行中显示 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];

谢谢, 克里斯蒂

【问题讨论】:

    标签: cocoa-touch ipad uiscrollview horizontal-scrolling vertical-scrolling


    【解决方案1】:

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

    【讨论】:

    • 哥们,我的应用程序不能使用三个 20 项目类,请提出其他方法
    【解决方案2】:

    -----更新-------

    - (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 个图像是可能的(横向模式)。根据您的要求修改代码。

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

    希望这会有所帮助。

    【讨论】:

    • 这里我只能得到数组中的最后一个图像,请帮助我无法找到它......
    • 同时显示的图像在屏幕上显示不正确
    • 如果你有这个不能正常工作,请提供任何其他示例代码
    • @Christina 此代码适用于我,它来自我的 Live 项目。你面临什么问题?如果可能,编辑您的问题并粘贴新代码,以便我们检查问题。
    • 我编辑了我的问题,请检查并回答,我正在使用我的一种方法在滚动视图中添加图像。请帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多