2014-11-23 20 views
0

我的scrollview不会调用它的viewdidscroll函数... 我很确定我的代码是“正确的”。 所以我恐怕在故事板面板的某处有一些选项。不会发布我在那里的一切截图。所以我希望有人有同样的问题。swift UIScrollView doesnt调用其“didscroll”函数

任何想法?

这是一个iPhone的ios应用程序在swift中。

import UIKit 

class ViewController: UIViewController, UIScrollViewDelegate { 

    @IBOutlet var scrollView: UIScrollView! 


    var imageGroup : [UIImage?] = [] 
    var containerViews : [UIImageView?] = [] 
    var containerView :UIImageView? 
    var imgWidthMult : CGFloat = 2.121875 
    let imageGroupCount : CGFloat? 
    let containerHeight : CGFloat? 
    let containerWidth : CGFloat? 
    var imageCounter : Int = 0 



    override func viewDidLoad() { 
     super.viewDidLoad() 
     // 1 
     imageGroup = [ 
      UIImage(named: "bg_001")!, 
      UIImage(named: "bg_002")!, 
      UIImage(named: "bg_003")!, 
      UIImage(named: "bg_004")!, 
      UIImage(named: "bg_005")!, 
      UIImage(named: "bg_006")!, 
      UIImage(named: "bg_007")!, 
      UIImage(named: "bg_008")!, 
      UIImage(named: "bg_009")!, 
      UIImage(named: "bg_010")!, 
      UIImage(named: "bg_011")!, 
      UIImage(named: "bg_012")!, 
      UIImage(named: "bg_013")!, 
      UIImage(named: "bg_014")!, 
      UIImage(named: "bg_015")!, 
      UIImage(named: "bg_016")!, 
      UIImage(named: "bg_017")!, 
      UIImage(named: "bg_018")!, 
      UIImage(named: "bg_019")!, 
      UIImage(named: "bg_020")! 
     ] 

     let imageGroupCount = CGFloat(imageGroup.count) 
     println(imageGroupCount) 


     // 3 
     for i in 0..<imageGroup.count { 
      containerViews.append(nil) 
     } 

     // 4 

     let imagesScrollViewSize = UIScreen.mainScreen().applicationFrame; 
     scrollView.contentSize = CGSizeMake(imagesScrollViewSize.height * imgWidthMult * CGFloat(imageGroup.count), imagesScrollViewSize.height) 

     // 5 
     let containerframe = UIScreen.mainScreen().applicationFrame; 
     let containerHeight : CGFloat = containerframe.height 
     let containerWidth : CGFloat = (imgWidthMult * containerHeight) 

     loadVisibleImages() 

     println("containerWidth") 
     println(containerWidth) 
     println(containerframe.size) 
     println(scrollView.contentSize) 




     return 
    } 

    // this loads images and should be adjusted and taken out of local scope 


    func loadImage (imageCounter:Int) { 
     let containerframe = UIScreen.mainScreen().applicationFrame; 
     let containerHeight : CGFloat = containerframe.height 
     let containerWidth : CGFloat = (imgWidthMult * containerHeight) 

     if imageCounter < 0 || imageCounter >= imageGroup.count { 
      // If it's outside the range of what you have to display, then do nothing 
      return 
     } 

     // 1 
     if let containerView = containerViews[imageCounter] { 
      // Do nothing. The view is already loaded. 
     } else { 
      // 2 
      var frame = UIScreen.mainScreen().applicationFrame; 
      frame.origin.x = frame.size.height * CGFloat(imageCounter) * 2.121875 
      frame.origin.y = 0.0 
      frame.size = CGSize(width: containerWidth, height: containerHeight) 

      // 3 
      var newcontainerView = UIImageView(image: imageGroup[imageCounter]) 
      newcontainerView.contentMode = .ScaleAspectFit 
      newcontainerView.frame = frame 
      scrollView.addSubview(newcontainerView) 


      containerViews[imageCounter] = newcontainerView 
     } 

    } 

    func purgeImage(imageCounter:Int) { 

     if imageCounter < 0 || imageCounter >= imageGroup.count { 
      // If it's outside the range of what you have to display, then do nothing 
      return 
     } 

     // Remove a page from the scroll view and reset the container array 
     if let containerView = containerViews[imageCounter] { 
      containerView.removeFromSuperview() 
      containerViews[imageCounter] = nil 
     } 

    } 


    func loadVisibleImages() { 


     // First, determine which page is currently visible 
     let containerframe = UIScreen.mainScreen().applicationFrame; 
     let containerHeight : CGFloat = containerframe.height 
     let containerWidth : CGFloat = (imgWidthMult * containerHeight) 
     let pageWidth = (containerWidth * 2.121875) 
     let imageCounter = Int(floor(scrollView.contentOffset.x/pageWidth)) 


     println(imageCounter) 

     // Update the page control 

     // Work out which pages you want to load 
     let firstPage = imageCounter - 1 
     let lastPage = imageCounter + 1 


     // Purge anything before the first page 
     for var index = 0; index < firstPage; ++index { 
      purgeImage(index) 
     } 

     // Load pages in our range 
     for var index = firstPage; index <= lastPage; ++index { 
      loadImage(index) 
     } 

     // Purge anything after the last page 
     for var index = lastPage+1; index < imageGroup.count; ++index { 
      purgeImage(index) 
     } 
     println("loadVisibleImages") 
    } 





    func scrollViewDidScroll(scrollView: UIScrollView) { 
     // Load the pages that are now on screen 
     println("did scroll") 
     loadVisibleImages() 
     println("did scroll") 

    } 




    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

} 

回答

3

您需要将视图控制器指定为滚动视图的委托。在您的viewDidLoad的某处添加:

scrollView.delegate = self 
+0

昨晚发现它很晚。以为我回答了这个。 这可能也有效。 我使用: 故事板模式=>连接督察=>委托=>指向viewcontroller – 2014-11-23 14:33:07