title: Scrolling images by dragging UISlider
body: <p>Iam beginner in IOS Dev. asking about a way to make <code>UIScrollView</code> images scrolls by dragging <code>UISlider</code> . This is my case :</p>
  
  <p><strong>Required case :</strong></p>
  
  <p>when dragging slider , images scrolls on <code>UIScrollView</code> . also when scrolling images , slider changes its value accordingly .</p>
  
  <p><strong>Actual case :</strong></p>
  
  <p>when scrolling images , slider changes its value accordingly <strong>BUT</strong> when dragging slider images <strong>DONOT</strong> Scroll .</p>
  
  <p><strong>Here is my code , I wish any one tell me how to scrolling when slider drags .</strong></p>
  
  <p>Slider <code>IBAction</code></p>
  
  <pre><code>- (IBAction)sliding:(UISlider *)sender{
      int slider_value = (int)slider.value;
      NSString *current_page = [[NSString alloc] initWithFormat:@"%i",slider_value];
      current_page_lbl.text = current_page ;
      [self loadPage:slider_value];
      [self loadVisiblePages];
  }
  </code></pre>
  
  <p>methods for UIScrollview :</p>
  
  <pre><code>- (void)loadPage:(NSInteger)page {
          if (page &lt; 0 || page &gt;= self.pageImages.count) {
              return;
          }
  
          UIView *pageView = [self.pageViews objectAtIndex:page];
          if ((NSNull*)pageView == [NSNull null]) {
  
              CGRect frame = self.scrollView.bounds;
              frame.origin.x = frame.size.width * page;
              frame.origin.y = 0.0f;
  
              UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
              newPageView.contentMode = UIViewContentModeScaleAspectFit;
              newPageView.frame = frame;
              [self.scrollView addSubview:newPageView];
  
              [self.pageViews replaceObjectAtIndex:page withObject:newPageView];
          }
      }
  
  - (void)purgePage:(NSInteger)page {
      if (page &lt; 0 || page &gt;= self.pageImages.count) {
          // If it's outside the range of what you have to display, then do nothing
          return;
      }
  
  
      UIView *pageView = [self.pageViews objectAtIndex:page];
      if ((NSNull*)pageView != [NSNull null]) {
          [pageView removeFromSuperview];
          [self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
      }
  }
  </code></pre>
  
  <p>To loadVisiblePage</p>
  
  <pre><code>- (void)loadVisiblePages {
  
      CGFloat pageWidth = self.scrollView.frame.size.width;
      NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
      NSLog(@"page loaded is %d",page);
  
      self.pageControl.currentPage = page; 
  
      NSInteger firstPage = page - 1;
      NSInteger lastPage = page + 1;
  
      for (NSInteger i=0; i&lt;firstPage; i++) {
           [self purgePage:i];
      }
  
      for (NSInteger i=firstPage; i&lt;=lastPage; i++) {
           [self loadPage:i];
           slider.value = 21-i ;
           int slider_value = (int)slider.value;
           NSString *current_page = [[NSString alloc] initWithFormat:@"%i",21-slider_value];
           current_page_lbl.text = current_page ;
      }
  
      for (NSInteger i=lastPage+1; i&lt;self.pageImages.count; i++) {
           [self purgePage:i];
      }
  }
  
  
  - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
      [self loadVisiblePages];
  }
  </code></pre>