【问题标题】:ceate array of image in UIscrollview?在 UIscrollview 中创建图像数组?
【发布时间】:2011-09-28 02:46:27
【问题描述】:

我想在下面的代码中创建图像数组而不是一个图像

- (void)viewDidLoad 
    {
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
        scrollView.contentSize = CGSizeMake(550, 800);
        NSString *imageName = [NSString stringWithFormat:@"page-1.jpg" ];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        [scrollView addSubview:imageView];
        imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
        [imageView release];
        scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
        [self.view addSubview:scrollView];
        [scrollView release];
        [super viewDidLoad];
    }

我如何创建它?并部署滚动视图的另一个功能。

【问题讨论】:

  • 你能详细说明一下吗?你到底在找什么?
  • 您想在滚动视图中动态添加图像吗?如果是,你想让图像水平还是垂直?要刷图吗?
  • 在上面现在只使用一个图像现在我想创建图像数组。滚动双方。并通过捏合或点击事件添加缩放。

标签: iphone xcode cocoa-touch ipad uiview


【解决方案1】:

我稍微更改了您的代码以添加 10 张图片,而不是 1 张。这是您想要的吗?

- (void)viewDidLoad 
{

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];

    int numberOfImages = 10;
    CGFloat currentX = 0.0f;

    for (int i=1; i <= numberOfImages; i++) {

        // create image
        NSString *imageName = [NSString stringWithFormat:@"page-%d.jpg", i];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        // put image on correct position
        CGRect rect = imageView.frame;
        rect.origin.x = currentX;
        imageView.frame = rect;

        // update currentX
        currentX += imageView.frame.size.width;

        [scrollView addSubview:imageView];
        [imageView release];
    }

    scrollView.contentSize = CGSizeMake(currentX, 800);

    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    [self.view addSubview:scrollView];
    [scrollView release];
    [super viewDidLoad];
}

希望对你有帮助!

【讨论】:

  • currentX 是滚动视图中图像将出现的位置。启用页面滚动,设置scrollView.pagingEnabled = YES;
  • 感谢您的帮助。您能告诉我如何在上面的代码中使用图像实现缩放功能吗?或者当点击图像然后从 url 或 web 服务上传相同的更大图像。
猜你喜欢
  • 1970-01-01
  • 2011-01-30
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-14
  • 2013-10-21
相关资源
最近更新 更多