1

I have created a simple image scrolling with UIPageControl and UIScrollView, I have three pages ! so what I need is when user scrolls page 0 to left , actually scroll should move to the page 3 and vice versa . I have checked Apple sample code but it was so complicated !, here is my codes :

- (void)setupScrollView {


    _pageControl.currentPage = 0;
    _pageControl.numberOfPages = 3;
    [_scrollView setContentSize:CGSizeMake(_scrollView.frame.size.width * _pageControl.numberOfPages, _scrollView.frame.size.height)];
    _scrollView.delegate = self;

    [self createPageWithImage:_image1 forPage:0];
    [self createPageWithImage:_image2 forPage:1];
    [self createPageWithImage:_image3 forPage:2];


}

    - (void)createPageWithImage:(UIImageView *)frameImage forPage:(int)page
    {
        UIView *newView = [[UIView alloc] initWithFrame: CGRectMake(_scrollView.frame.size.width * page, 0, _scrollView.frame.size.width, _scrollView.frame.size.height)];
        [newView addSubview: frameImage];
        [_scrollView addSubview: newView];

    }


    - (void)scrollViewDidScroll: (UIScrollView *) sView
    {

        CGFloat offset = _scrollView.contentOffset.x;
        CGFloat pageSize = _scrollView.frame.size.width;

        int page = floor((offset + (pageSize/2)) / pageSize);
        _pageControl.currentPage = page;



    }





- (IBAction)changeThePage
{

    CGRect pageRect = CGRectMake(_pageControl.currentPage * _scrollView.frame.size.width, 0, _scrollView.frame.size.width, _scrollView.frame.size.height);

    [_scrollView scrollRectToVisible: pageRect animated: YES];
}
iOS.Lover
  • 5,923
  • 21
  • 90
  • 162

1 Answers1

2

Adding the methods which I have changed. I have commented wherever necessary.

- (void)setupScrollView {
    _pageControl.currentPage = 0;
    _pageControl.numberOfPages = 3 ;
    //Add 2 more pages.
    [_scrollView setContentSize:CGSizeMake(_scrollView.frame.size.width * (_pageControl.numberOfPages + 2), _scrollView.frame.size.height)];
    _scrollView.delegate = self;

    // Seriously recommend this for this type of apps.
    _scrollView.pagingEnabled = YES;

    // Do not instantiate imageviews. Send only image names as string.
    // Add last image at beginning of scroll view.
    [self createPageWithImageName:@"imageName3" forPage:0];

    // Increase page number of existing images.
    [self createPageWithImageName:@"imageName1" forPage:1];
    [self createPageWithImageName:@"imageName2" forPage:2];
    [self createPageWithImageName:@"imageName3" forPage:3];

    //Add first image at end of scroll view
    [self createPageWithImageName:@"imageName1" forPage:4];

    // Show first image but present in second page.
    [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0) animated:NO];
}

// Instead of sending image views, send image name. Create image view inside this method.
// This is because, since we are adding two more images, separate image view needs
// to be created. Otherwise, same image view will be used and one added at the end 
// will be used as the frame of the image.
- (void)createPageWithImageName:(NSString *)imageName forPage:(int)page
{

    UIView *newView = [[UIView alloc] initWithFrame: CGRectMake(_scrollView.frame.size.width * page, 0, _scrollView.frame.size.width, _scrollView.frame.size.height)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:newView.bounds];
    imageView.image = [UIImage imageNamed:imageName];

    [newView addSubview: imageView];
    [_scrollView addSubview: newView];

}

- (void)scrollViewDidScroll: (UIScrollView *) sView
{
    CGFloat offset = _scrollView.contentOffset.x;
    CGFloat pageSize = _scrollView.frame.size.width;

    int page = floor((offset + (pageSize/2)) / pageSize);
    if (page == 0) {
        page = _pageControl.numberOfPages - 1;
    }
    else if (page == _pageControl.numberOfPages + 1) {
        page = 0;
    }
    else {
        page = page - 1;
    }
    _pageControl.currentPage = page;

    // If present in scroll view's first page, move it to second last page
    if (offset < pageSize) {
        [_scrollView setContentOffset:CGPointMake(pageSize * 3 + offset, 0) animated:NO];
    }
    // If present in scroll view's last page, move it to second page.
    else if (offset >= pageSize * (_pageControl.numberOfPages + 1)) {
        CGFloat difference = offset - pageSize * _pageControl.numberOfPages;
        [_scrollView setContentOffset:CGPointMake(difference, 0) animated:NO];
    }
}

The method of changeThePage is not needed for this code.

Hope this answer helps you.

Prasad
  • 5,946
  • 3
  • 30
  • 36
  • Is it happening only in page 0? I am not sure what is it. Just try this once. Inside the method of `- (void)createPageWithImage:(UIImageView *)frameImage forPage:(int)page`, just add this line and see. `[frameImage setFrame:newView.bounds];` – Prasad Aug 30 '14 at 17:31
  • It is not happening for me. Some other part of your code is causing that problem. Debug once. Specifically parts of code where you are using scroll view. – Prasad Aug 30 '14 at 17:38
  • Sure. You can send me. – Prasad Aug 30 '14 at 17:41
  • Are you sure you have sent me correct link? Because in this link, I can see only one panoramic image. And also, I can't see any of your previous code? – Prasad Aug 30 '14 at 18:00
  • Sorry. Found the mistake. I was adding the same image view again. Hence, it was vanishing in the first place. When I did it, I was creating image views inside the parameters, and hence I didn't face the problem. But silly me that I didn't look into this problem. Presently, I have edited the code and it is working. – Prasad Aug 30 '14 at 18:53
  • Brilliant ! thanks !! but I actually I have to mentioned UIImageView not image name , because images will be downloaded from server , it's not local ! I will look into it , thanks again – iOS.Lover Aug 30 '14 at 19:08
  • Yup. No problem. It is my duty that if I give answer, I do have to give it correctly. Regarding images, you can download it from server and then pass it as parameter instead of image view or else, you can create separate image view for them and then send it. Anyways, it was nice helping you. :) – Prasad Aug 30 '14 at 19:11
  • Sorry for keep bothering ! can I have your email , I faced with a tiny problem here ? – iOS.Lover Aug 30 '14 at 19:56
  • I don't check my mail so frequently. Why don't you post it as separate question? Besides you will get quick answers if you post it here. – Prasad Aug 31 '14 at 06:13
  • Thanks check this out : http://stackoverflow.com/questions/25590345/having-issue-with-infinite-uiscrollview-uipagecontrol-infinite-scrolling – iOS.Lover Aug 31 '14 at 08:44