【问题标题】:Add 10 different UIImages in UIScrollView在 UIScrollView 中添加 10 个不同的 UIImages
【发布时间】:2011-07-29 16:21:44
【问题描述】:

我正在尝试在UIImageView 下添加各种UIImages 并允许它们使用UIScrollView 滚动。我不知道如何在UIImageView 下添加各种图像并让它们滚动。 下面是我在UIImageView 上添加图像并使其可滚动的代码。

- (void)viewDidLoad {

    [super viewDidLoad];
    UIImage *image = [UIImage imageNamed:@"ae.jpg"];
    imageView = [[UIImageView alloc]initWithImage:image];
    imageView.frame = [[UIScreen mainScreen] bounds];
    imageView.contentMode = (UIViewContentModeScaleAspectFit);
    imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    imageView.backgroundColor = [UIColor clearColor];



    UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    scrollView.contentMode = (UIViewContentModeScaleAspectFit);

    scrollView.contentSize = CGSizeMake(image.size.width,960);
    scrollView.pagingEnabled = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.alwaysBounceVertical = NO;
    scrollView.alwaysBounceHorizontal = NO;
    scrollView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    scrollView.maximumZoomScale = 2.5;
    scrollView.minimumZoomScale = 1;
    scrollView.clipsToBounds = YES;
   [scrollView addSubview:imageView];
   [image release];
    [imageView release];
   [self.view addSubview:scrollView];
}

【问题讨论】:

  • 您可能希望在同一个 UIScrollView 中使用多个 UIImageView。但目前还不清楚你想要它做什么。
  • 我想在一张下方添加各种图像,例如想要在“ae.png”下方添加另一张图像并允许它滚动。可以说目前它只有一个图像,但是如果我想在 UIIImageView 中添加一组图像,可以吗?
  • 只使用多个 UIImageViews。
  • 好的。但是,您将如何以编程方式确定图像的位置?

标签: iphone objective-c uiscrollview uiimageview uiimage


【解决方案1】:

这个想法基本上很简单。假设您想在 UIScrollView 中放置 3 张图像。 每个图像都是 300x300。在这种情况下,您将拥有带框架的滚动视图:

scrollView.contentSize = CGSizeMake(image.size.width,900);

对于每张图片,您都必须拥有带有适当框架的 UIImageView:

imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, 300, 300)];
imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 300, 300, 300)];
imgView3 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 600, 300, 300)];
imgView1.image = [UIImage imageNamed:@"ProperName.png"];
...

(注意yOrigin(CGRectMake中的第二个值)) 然后像你一样:

[scrollView addSubview:imgView1];
[scrollView addSubview:imgView2];
[scrollView addSubview:imgView3];
[imgView1 release];
[imgView2 release];
[imgView3 release];

当然,这是一个简短的代码,你会优化它;)

【讨论】:

  • 您建议进行什么样的优化?因为有 20 个 uiview,每个都包含 1 个 UIImageview(带阴影和边框)和 2 个 UILabel(没有文本效果),所以我的 UIScrollView 滚动不流畅。
  • 好吧,我还没有遇到过这种问题,但是我会尝试实现一些页面重用算法(UITableView 采用这种方法进行单元重用)。这样,在内存中,您将始终拥有有限数量的页面(以及相应的资源)。显然,您必须在滚动时以动态方式填写内容。您可以在此处阅读我的答案(stackoverflow.com/questions/8374907/… 浏览答案的第 n.2 部分)。在那里,您将获得有关如何节省内存的可能想法之一。祝你好运。
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多