从apple开发者网站获得了一个viewscroller的代码。
@synthesize scrollView1, scrollView2;
const CGFloat kScrollObjHeight = 467.0;
const CGFloat kScrollObjWidth = 320.0;
const NSUInteger kNumImages = 6;
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
通过向左或向右拉动图像,它可以很好地工作。但我想上下拉动图像来改变它们。
任何人都可以帮我解决这个问题吗?代码中的这一部分看起来好像控制了方向,但我不知道如何改变它。
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
非常感谢任何帮助,谢谢!
比利
答案 0 :(得分:2)
要将滚动“方向”从水平更改为垂直,您基本上应该交换处理x和y坐标的方式。代码如下:
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curYLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(0, curYLoc);
view.frame = frame;
curYLoc += (kScrollObjHeight);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake([scrollView1 bounds].size.width, kNumImages * kScrollObjHeight)];
}