title: IOS image viewer in swift, programmatically
body: <p>i am trying to make an imageViewer using this toturial 
  <a href="http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content" rel="nofollow">this toturial</a></p>
  
  <p>i want to use the "PagedScrollViewController" example and make it using swift, 
  i am able to display images on screen, but i have issues with the scroll.</p>
  
  <p>the scroll is not stoping between pages/photos, it scrolling freely, so that way <code>loadVisiblePages</code> is always called each pixel that i scroll.</p>
  
  <p>probably i am missing something, can you help me with this please? 
  here is what i am doing :</p>
  
  <p>thank you</p>
  
  <pre><code>import UIKit
  
  class PagedScrollViewController:UIViewController,UIScrollViewDelegate {
  
  
  var pageImages:[UIImage] = [UIImage]()
  var pageViews:[UIView?] = [UIView]()
  
  var scrollView:UIScrollView = UIScrollView()
  var pageControl:UIPageControl = UIPageControl()
  
  
  func loadVisiblePages() {
      // First, determine which page is currently visible
      var pageWidth:CGFloat = self.scrollView.frame.size.width;
  
      var page = Int(floor((self.scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth * 2.0)));
  
      // Update the page control
      self.pageControl.currentPage = page;
  
      // Work out which pages we want to load
      var firstPage = page - 1;
      var lastPage = page + 1;
  
      // Purge anything before the first page
      for (var i=0; i&lt;firstPage; i++) {
          println("1) purge index \(i)")
          self.purgePage(i)
      }
      for (var i=firstPage; i&lt;=lastPage; i++) {
          println("2) load index \(i)")
          self.loadPage(i)
      }
      for (var i = lastPage+1 ; i &lt; self.pageImages.count ; i++) {
          println("3) purge index \(i)")
          self.purgePage(i)
      }
  }
  //
  func loadPage(page:Int) {
      if page &lt; 0 || page &gt;= self.pageImages.count {
          // If it's outside the range of what we have to display, then do nothing
          return;
      }
  
      // Load an individual page, first seeing if we've already loaded it
      var pageView:UIView? = self.pageViews[page];
      if pageView == nil {
          var frame:CGRect = self.scrollView.bounds;
          //frame.origin.x = frame.size.width * CGFloat(page)
          frame.origin.x = 320.0 * CGFloat(page)
          frame.origin.y = 0.0
  
          var newPageView:UIImageView = UIImageView(image: self.pageImages[page])
          newPageView.contentMode = UIViewContentMode.ScaleAspectFit;
          newPageView.frame = frame;
          self.scrollView.addSubview(newPageView)
          self.pageViews[page] = newPageView
      }
  }
  
  func purgePage(page:Int) {
      if page &lt; 0 || page &gt;= self.pageImages.count {
          // If it's outside the range of what we have to display, then do nothing
          return;
      }
  
      // Remove a page from the scroll view and reset the container array
      var pageView:UIView? = self.pageViews[page];
      if pageView != nil {
          pageView?.removeFromSuperview()
          self.pageViews[page] = UIView()//nil
      }
  }
  
  
  
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
  
      self.title = "Paged";
  
      self.view.backgroundColor = UIColor.blueColor()
  
      // Set up the image we want to scroll &amp; zoom and add it to the scroll view
      self.pageImages.append(UIImage(named: "first.png"))
      self.pageImages.append(UIImage(named: "second.png"))
      self.pageImages.append(UIImage(named: "first.png"))
      self.pageImages.append(UIImage(named: "second.png"))
      self.pageImages.append(UIImage(named: "first.png"))
  
      var pageCount = self.pageImages.count
  
      self.scrollView.delegate = self
      //self.tableView.showsHorizontalScrollIndicator = false
      //self.tableView.showsVerticalScrollIndicator = false
  
      // Set up the page control
      self.pageControl.currentPage = 0;
      self.pageControl.numberOfPages = pageCount;
  
      self.pageControl.setTranslatesAutoresizingMaskIntoConstraints(false)
      self.scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
  
      self.view.addSubview(self.pageControl)
      self.view.addSubview(self.scrollView)
  
  
      //Set layout
      var viewsDict = Dictionary &lt;String, UIView&gt;()
      viewsDict["control"] = self.pageControl;
      viewsDict["scrollView"] = self.scrollView;
  
      self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[scrollView]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
      self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[control]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
      self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[scrollView(400)]-[control]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
  
  
      // Set up the array to hold the views for each page
      for (var i = 0; i &lt; pageCount; ++i) {
          self.pageViews.append(nil)
  
      }
  }
  
  
  override func viewDidAppear(animated:Bool) {
      super.viewDidAppear(animated)
  
      // Set up the content size of the scroll view
      var pagesScrollViewSize:CGSize = self.scrollView.frame.size;
      self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(self.pageImages.count), pagesScrollViewSize.height);
  
      // Load the initial set of pages that are on screen
      self.loadVisiblePages()
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
  
      //        self.scrollView = nil
      //        self.pageControl = nil
      //        self.pageImages = nil
      //        self.pageViews = nil
  }
  
  func scrollViewDidScroll(scrollView:UIScrollView ) {
      // Load the pages which are now on screen
      self.loadVisiblePages()
      println("scrollViewDidScroll")
  }
  
  
  
  }
  </code></pre>