如何修复当前页面不显示真实? [迅速]

时间:2016-06-06 04:24:10

标签: ios swift uipagecontrol

为什么页面控制中的当前页面没有显示正确的输出?

第1页和第2页以一个点显示?图片在这里: http://i.stack.imgur.com/498ap.pnghttp://i.stack.imgur.com/41kdg.png

最后一页是第6页显示在第5点,不是最后一个点吗?图片:http://i.stack.imgur.com/NP9u1.png

我的代码在这里:

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

let totalPages = 6
let sampleBGColors: Array<UIColor> = [UIColor.redColor(), UIColor.yellowColor(), UIColor.greenColor(), UIColor.magentaColor(), UIColor.orangeColor(), UIColor.lightGrayColor()]    @IBOutlet weak var scrollView: UIScrollView!

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    configureScrollView()
    configurePageControl()
}

func configureScrollView() {
    // Enable paging.
    scrollView.pagingEnabled = true

    // Set the following flag values.
    scrollView.showsHorizontalScrollIndicator = false
    scrollView.showsVerticalScrollIndicator = false
    scrollView.scrollsToTop = false

    // Set the scrollview content size.
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * CGFloat(totalPages), scrollView.frame.size.height)

    // Set self as the delegate of the scrollview.
    scrollView.delegate = self

    // Load the TestView view from the TestView.xib file and configure it properly.
    for i in 0 ..< totalPages {
        // Load the TestView view.
        let testView = NSBundle.mainBundle().loadNibNamed("TestView", owner: self, options: nil)[0] as! UIView

        // Set its frame and the background color.
        testView.frame = CGRectMake(CGFloat(i) * scrollView.frame.size.width, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height)
        testView.backgroundColor = sampleBGColors[i]

        // Set the proper message to the test view's label.
        let label = testView.viewWithTag(1) as! UILabel
        label.text = "Page #\(i + 1)"

        // Add the test view as a subview to the scrollview.
        scrollView.addSubview(testView)
    }
}


func configurePageControl() {
    // Set the total pages to the page control.
    pageControl.numberOfPages = totalPages

    // Set the initial page.
    pageControl.currentPage = 0
}


// MARK: UIScrollViewDelegate method implementation

func scrollViewDidScroll(scrollView: UIScrollView) {
    // Calculate the new page index depending on the content offset.
    let currentPage = floor(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.size.width);

    // Set the new page index to the page control.
    pageControl.currentPage = Int(currentPage)
}


// MARK: IBAction method implementation

@IBAction func changePage(sender: AnyObject) {
    // Calculate the frame that should scroll to based on the page control current page.
    var newFrame = scrollView.frame
    newFrame.origin.x = newFrame.size.width * CGFloat(pageControl.currentPage)
    scrollView.scrollRectToVisible(newFrame, animated: true)

}

请帮帮我!谢谢。

抱歉我的英语不好。

1 个答案:

答案 0 :(得分:0)

在UIScrollViewDelegate的实现 scrollViewDidEndScrollingAnimation scrollViewDidEndDecelerating 中更改 pageControl.currentPage ,并使用scrollView&#39;改进了计算。宽度,而不是屏幕的宽度:

// MARK: UIScrollViewDelegate method implementation
func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) {
    // Calculate the new page index depending on the content offset.
    let currentPage = floor(scrollView.contentOffset.x / scrollView.bounds.size.width)
    // Set the new page index to the page control.
    pageControl.currentPage = Int(currentPage)
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView){
    scrollViewDidEndScrollingAnimation(scrollView)
}