【问题标题】:Swift: Updating UIPageControl from ScrollViewSwift:从 ScrollView 更新 UIPageControl
【发布时间】:2016-06-11 04:20:32
【问题描述】:

我似乎在让 UIPageControl 工作时遇到了一些问题。

我有一个包含 ScrollView 的 ViewController。此 ScrollView 加载可滑动的 nib 文件。见图片:

这里是加载这些的代码:

self.addChildViewController(vc0)
self.displayReportView.addSubview(vc0.view)
vc0.didMoveToParentViewController(self)
        
var frame1 = vc1.view.frame
frame1.origin.x = self.view.frame.size.width
vc1.view.frame = frame1
self.addChildViewController(vc1)
self.displayReportView.addSubview(vc1.view)
vc1.didMoveToParentViewController(self)

// And so on
...

这可以正常滚动,因为它们可以正确滚动等等。

现在,在 ViewController(一个持有滚动视图的)上,我添加了委托:

UIScrollViewDelegate

创建了一些变量:

 var frame: CGRect = CGRectMake(0, 0, 0, 0)
 var colors:[UIColor] = [UIColor.redColor(), UIColor.blueColor(), UIColor.greenColor(), UIColor.yellowColor()]
 var pageControl : UIPageControl = UIPageControl(frame: CGRectMake(50, 300, 200, 20))

我添加了一些需要的功能:

func configurePageControl() {
    // The total number of pages that are available is based on how many available colors we have.
    
    self.pageControl.numberOfPages = 4
    self.pageControl.currentPage = 0
    self.pageControl.tintColor = UIColor.redColor()
    self.pageControl.pageIndicatorTintColor = UIColor.blackColor()
    self.pageControl.currentPageIndicatorTintColor = UIColor.greenColor()
    self.view.addSubview(pageControl)
    
}


// MARK : TO CHANGE WHILE CLICKING ON PAGE CONTROL
func changePage(sender: AnyObject) -> () {
    let x = CGFloat(pageControl.currentPage) * displayReportView.frame.size.width
    displayReportView.setContentOffset(CGPointMake(x, 0), animated: true)
}


func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    
    let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
    pageControl.currentPage = Int(pageNumber)
}

现在,当我运行应用程序时,滚动视图点会显示,但是当我滑动时它们不会更新。

问题

如何更新点以反映显示的视图?

如果您需要我的代码中的其他内容来查看功能,请告诉我。

【问题讨论】:

    标签: ios swift uiscrollview uipagecontrol


    【解决方案1】:

    如果您有 分页 滚动视图,您当然可以按照您的描述进行操作;我有一个使用此代码的示例:

    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let x = scrollView.contentOffset.x
        let w = scrollView.bounds.size.width
        pageControl.currentPage = Int(x/w)
    }
    

    除了你的round,你的代码看起来很多,这让我认为你的代码应该工作。这让我觉得其他东西只是配置错误。 这是一个分页滚动视图吗?您是否记得将此对象设为滚动视图的delegate?使用日志记录或断点来确保您的 scrollViewDidEndDecelerating 甚至首先被调用。

    但是,我想指出的是,您所描述的配置实际上是 UIPageViewController 免费为您提供的——一个带有视图控制器视图的滚动视图,以及一个页面控件——所以你可能想要使用它。

    【讨论】:

    • 这是一个可下载的分页水平滚动视图和页面控件的工作示例,因此您可能想自己尝试一下,然后看看您自己的代码有何不同:github.com/mattneub/Programming-iOS-Book-Examples/blob/…
    • 嗨,马特,感谢您的回复。我正在努力让它继续工作,我在太阳穴上的小虚荣就要破裂了。模糊......我已经在这里上传了这个项目:jumpshare.com/v/2Lq5U0KW2ExBWezcw7o3 - 你认为你可以看看并给我一些指示吗? (跪下,祈祷和乞求)
    • 很抱歉,如果它是超级厚颜无耻地要求您查看该项目。为了澄清,我试图得到它,以便当用户向左或向右滚动时,页面控件会相应地更新。
    • 嗨@JamesG - 我下载了你的项目并查看了正确的答案就在我的答案中。请阅读我所说的(在第二段中)并考虑一下!提示:通过在您的 ViewController 的 viewDidLoad 中添加一行代码,我能够让您的项目正常工作(即页面控件更新)。
    • :) 很抱歉这么顽固,但我是一名教师——我的工作就是折磨你学习!
    【解决方案2】:

    我会用UICollectionView 替换滚动视图。这样您就可以免费获得分页,而且效果会更好,因为分页可以开箱即用,您无需计算帧偏移量。

    一定要设置collectionView.pagingEnabled = true

    要获取当前页码,请执行collectionView.indexPathsForVisibleItems().first?.item

    更改页面:

    collectionView.scrollToItemAtIndexPath(newIndexPath, atScrollPosition: CenteredHorizontally, animated: true)

    【讨论】:

    • 我宁愿找到一种方法来利用我所拥有的。不过感谢您的建议,下一个应用程序会考虑这个。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多