Swift - 滚动视图回到第一个视图 - scrollViewDidScroll未调用

时间:2015-07-07 00:27:03

标签: ios swift

我想在我的代码中添加一个循环页面。当滚动视图到达最后一页时,下一页应该是第一页,反之亦然。

在我的项目中,我有一个带有视图控制器的主故事板,其中放置了一个滚动视图和两个包含两个页面的xib文件。

一切都按原样运作但是我无法想办法如何在两种方式(后退和前进)中进行无限滚动

如果我添加了函数scrollViewDidScroll,它似乎没有被调用。

任何想法? 这是我的代码:

class ViewController: UIViewController, UIScrollViewDelegate {
    @IBOutlet weak var scrollView: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set Scroll Bar Hidden
        scrollView.showsHorizontalScrollIndicator = false;
        scrollView.showsVerticalScrollIndicator = false



        // Create the views used in the swipe container view
        let NewsPage :NewsViewController =  NewsViewController(nibName: "NewsViewController", bundle: nil);
        let MusicPage :MusicViewController =  MusicViewController(nibName: "MusicViewController", bundle: nil);


        // Add in each view to the container view hierarchy

        self.addChildViewController(MusicPage);
        self.scrollView!.addSubview(MusicPage.view);
        MusicPage.didMoveToParentViewController(self);

        self.addChildViewController(NewsPage);
        self.scrollView!.addSubview(NewsPage.view);
        NewsPage.didMoveToParentViewController(self);


        // Set up the frames of the view controllers to align with eachother inside the container view

        var adminFrame :CGRect = NewsPage.view.frame;
        adminFrame.origin.x = adminFrame.width;
        MusicPage.view.frame = adminFrame;


        // Set the size of the scroll view that contains the frames

        let scrollWidth: CGFloat  = 2 * self.view.frame.width
        let scrollHeight: CGFloat  = self.view.frame.size.height
        self.scrollView!.contentSize = CGSizeMake(scrollWidth, scrollHeight);

        NSLog("%f",scrollHeight);
    }

   func scrollViewDidScroll(scrollView: UIScrollView) {

        if (scrollView.contentOffset.x > scrollView.frame.size.width){
            scrollView.setContentOffset(CGPointMake(0.0,0.0), animated: false)
        }
    }

    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
         NSLog("dragg");
    }

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


}

1 个答案:

答案 0 :(得分:0)

您是否设置了scrollView的委托?尝试添加到viewDidLaod

scrollView.delegate = self