开发者

ceate array of image in UIscrollview?

开发者 https://www.devze.com 2023-03-16 23:22 出处:网络
I want to create array of image instead of one image in below code - (void)viewDidLoad { UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];

I want to create array of image instead of one image in below code

- (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 | UIViewAutoresi开发者_C百科zingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
        [self.view addSubview:scrollView];
        [scrollView release];
        [super viewDidLoad];
    }

how i create it? and deploy another functionality of scroll view.


I changed your code a bit to add 10 images, instead of 1. Is this what you want?

- (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];
}

Hope it helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消