title: Swiping Images with Page Control in Iphone
body: <p>I am trying to make practice app where i can scroll images with page control. I am able to scroll images and able to include the page control. But the problem i face is i am not able to interlink the two. Meaning to say when I scroll the images, the page control is not affected and when i change the page control, the scrolling of the images is unaffected. </p>
  
  <p>I have referred to this: <a href="http://www.iosdevnotes.com/2011/03/uiscrollview-paging/">http://www.iosdevnotes.com/2011/03/uiscrollview-paging/</a> for the scrolling with page control.</p>
  
  <p>Viewcontroller.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @interface ViewController : UIViewController &lt;UIScrollViewDelegate&gt;{
      UIScrollView *scrollView;
      UIPageControl *pageControl;
  
      BOOL pageControlBeingUsed;
  }
  
  @property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
  @property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
  
  - (IBAction)changePage;
  
  @end
  </code></pre>
  
  <p>Viewcontroller.m</p>
  
  <pre><code>#import "ViewController.h"
  
  @interface ViewController ()
  
  @end
  
  @implementation ViewController
  
  @synthesize scrollView,pageControl;
  
  - (void)scrollViewDidScroll:(UIScrollView *)sender {
      if (!pageControlBeingUsed) {
          // Switch the indicator when more than 50% of the previous/next page is visible
          CGFloat pageWidth = self.scrollView.frame.size.width;
          int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
          self.pageControl.currentPage = page;
      }
  }
  - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
      pageControlBeingUsed = NO;
  }
  
  - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
      pageControlBeingUsed = NO;
  }
  - (void)viewDidLoad
  {
      [super viewDidLoad];
      NSArray *images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"1.jpeg"],[UIImage imageNamed:@"2.jpeg"],[UIImage imageNamed:@"3.jpeg" ], nil];
  
      self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
  
      for (int i = 0; i &lt; images.count; i++) {
          CGRect frame;
          frame.origin.x = self.scrollView.frame.size.width * i;
          frame.origin.y = 0;
          frame.size = self.scrollView.frame.size;
          UIImageView* imgView = [[UIImageView alloc] init];
          imgView.image = [images objectAtIndex:i];
          imgView.frame = frame;
          [scrollView addSubview:imgView];
      }
  
      self.pageControl.currentPage = 0;
      self.pageControl.numberOfPages = images.count;
  
  }
  
  - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
  }
  - (void)viewDidUnload {
      // Release any retained subviews of the main view.
      // e.g. self.myOutlet = nil;
      self.scrollView = nil;
      self.pageControl = nil;
  }
  
  - (IBAction)changePage{
          // update the scroll view to the appropriate page
          CGRect frame;
          frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
          frame.origin.y = 0;
          frame.size = self.scrollView.frame.size;
          [self.scrollView scrollRectToVisible:frame animated:YES];
          pageControlBeingUsed = YES;
  }
  
  @end
  </code></pre>
  
  <p>Need some guidance on this... Thanks.. </p>