如何制作pageControl滑动效果?滚动视图连续滑动至今

How to make the pageControl swipe effect? The scroll view slides continuously so far

我想用scrollView做滑动效果,我发现了一个项目,在代码或故事板中没有专门调用任何东西。我重复了代码,但在我的项目中,scrollView 只是连续滑动,它不会从一个 imageView 滑动到另一个。你知道怎么做吗?

这是代码,滚动响应触摸,pagecontrol 是一个简单的 iboutlet,但是没有滑动效果(滚动停止在触摸结束的地方,而不是返回或移动整个 imageview ):

class ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var pageControl: UIPageControl!
    @IBOutlet weak var scrollView: UIScrollView!

    var pageViews: [UIImageView?] = []
    var pageImages : [UIImage?] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        pageImages = [UIImage(named:"photo1.png"),
        UIImage(named:"photo2.png"),
        UIImage(named:"photo3.png"),
        UIImage(named:"photo4.png")]

        let pageCount = pageImages.count
        pageControl.currentPage = 0
        pageControl.numberOfPages = pageCount

        for _ in 0..<pageCount {
            pageViews.append(nil)
        }

        let pagesScrollViewSize = scrollView.frame.size
        println("pagesScrollViewSize : \(pagesScrollViewSize)")
        scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(pageCount), pagesScrollViewSize.height)
        loadVisiblePages()
    }

    //when scrolling
    func scrollViewDidScroll(scrollView: UIScrollView!){
        loadVisiblePages()
    }

    func loadVisiblePages(){
        let pageWidth = scrollView.frame.size.width
        let page = Int(floor( (scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth*2.0) ))
        println("page: \(page)")
        pageControl.currentPage = page
        let firstPage = page-1
        let lastPage = page+1
        //remove all the pages before firstPage
        for var index = 0; index < firstPage; ++index{
            purgePage(index)
        }
        //load pages in our range
        for var index = firstPage; index <= lastPage; ++index {
            loadPage(index)
        }
        //remove after lastPage
        for var index = lastPage+1; index < pageImages.count; ++index {
            purgePage(index)
        }
    }

    func loadPage(index:Int){
        if index < 0 || index >= pageImages.count {
            return
        }
        if let pageView = pageViews[index] {
            //already loaded
        }
        else {
            var frame = scrollView.bounds
            frame.origin.x = frame.size.width * CGFloat(index)
            frame.origin.y = 0.0
            println("\(frame)")

            var newImageView = UIImageView(image:pageImages[index])
            newImageView.contentMode = .ScaleAspectFit
            newImageView.frame = frame
            scrollView.addSubview(newImageView)

            pageViews[index] = newImageView
        }
    }

    func purgePage(index:Int){
        if index < 0 || index >= pageImages.count {
            return
        }
        if let pageView = pageViews[index] {
            pageView.removeFromSuperview()
            pageViews[index] = nil
        }
    }

}

UIPageControl 不执行页面滚动行为 - 它仅提供指示显示哪个页面的点(并且用户可以点击以在页面之间跳转)。使用 UIScrollViewpagingEnabled 属性 来逐页滚动,而不是连续滚动。

或者只使用 UIPageViewController 来为您处理几乎所有的滚动和分页业务。