向UIScrollView中添加UIImageViews

5

我目前正在研究如何将多个UIImageView添加到一个单独的UIScrollView中。 UIScrollView的大小将是任何一个UIImageViews的1.5倍。 我想为iPad应用程序中浏览一些小图像创建滚动效果。 有人知道如何实现吗?

2个回答

11
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(/*FRAME*/)]; // this makes the scroll view - set the frame as the size you want to SHOW on the screen
[scrollView setContentSize:CGSizeMake(/*SIZE OF THE CONTENT*/)]; // if you set it to larger than the frame the overflow will be hidden and the view will scroll

/* you can do this bit as many times as you want... make sure you set each image at a different origin */
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"/*IMAGE*/"]]; // this makes the image view
[imageView setFrame:CGRectMake(/*SET AS 2/3 THE SIZE OF scrollView AND EACH IMAGE NEXT TO THE LAST*/)]; // this makes the image view display where you want it and at the right size
[scrollView addSubview:imageView]; // this adds the image to the scrollview
/* end adding image */

[self.view addSubview:scrollView];

好的,明白了!我会尝试一下! - TheLearner
非常好的解释,谢谢伙计。有没有办法让滚动条始终可见? - TheLearner
或者可以通过一种方式使其动起来,以便用户知道他们可以滚动。 - TheLearner
好的解释!还要记得在将视图添加为子视图后释放已分配的视图。如果您想学习如何实现分页(对图像视图进行捕捉)和页面指示器以及如何释放视图外的图像,我建议您查看我给您的第二个链接。如果您想能够缩放图像,则可以查看第一个链接。 - LarsJK
是的,抱歉 - 由于我的 PHP 背景,我主要问题在于内存管理。我发现最好的方法是在你的元素周围放置[[...] autorelease] - 这样你就不必记得释放它们(直到你遇到 EXC_BAD_ACCESS 和各种问题... :p) - Thomas Clayson
是的,我也遇到了内存管理的问题,但苹果指南真的值得一读! - TheLearner

1

谢谢,伙计。我会尝试一下,但看起来很复杂! - TheLearner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接