我想在Scroll视图中显示数据库中的图像。此外,我想在一行中显示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];
谢谢,Christy
你看过Three20的照片库控件吗?有一个关于如何使用它的教程here
(由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)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个图像(横向模式)。根据您的要求修改相同的代码。
如果您需要进一步的帮助,请留言。
希望这能有所帮助。