imagepaginationuiscrollviewscrollviewxcode4.2

Scrollview paging with images?! xCode 4.2


I've been searching for long time how to add images into a scrollview and enable paging, so when i swipe left or right the next or previus images shows. i havent find anything that works yet, the only thing i found was this

Code below:

    - (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *colors = [NSArray arrayWithObjects:[UIColor orangeColor], [UIColor greenColor], [UIColor blueColor],[UIColor   redColor], nil];
    for (int i = 0; i < colors.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        UIView *subview = [[UIView alloc] initWithFrame:frame];
        subview.backgroundColor = [colors objectAtIndex:i];
        [self.scrollView addSubview:subview];

    }

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
}

Ofc this does enable paging but with colors only, i tryed change where the UIColor with [UIImage imageName: @"photo1.jpg"] but it doesnt work at all...

So after searching for a few days i need to ask how to do it, how to have some images inside a scroll view and have paging enabled

Thanks in advance for any help


Solution

  • I did find the answer my self after all. kinda proud to be honest because ive been looking for that soo long. anyway what i should have done is change first of all the UIColor at the array with UIImage.

    like this:

    NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:@"photo1.jpg"],[UIImage imageNamed:@"photo2.jpg"],[UIImage imageNamed:@"photo3.jpg"], nil];
    for (int i = 0; i < images.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;
    

    and change the UIView with an UIImageView and works like a charm!!!