如何在每页不同颜色的UIPageControl中更改分页点的颜色

时间:2015-10-09 05:16:00

标签: ios swift uikit uipagecontrol

我正在使用UIPageControl和UIScrollView来显示5个图像。我希望能够为每个页面(当选择它时)将当前页面指示器点颜色设置为不同的颜色。我已尝试在UIPageControl的Value Changed操作中设置currentPageIndicatorTintColor,但在更改为新颜色之前,这会短暂显示之前的颜色。

有没有人知道是否可以为每个点使用不同的颜色,以便显示无缝?

注意:这不是How can i change the color of pagination dots of UIPageControl?的副本,因为该问题是关于将所有点更改为具有相同的颜色,而我想在页面当前页面时更改单个页面点以使其具有不同的颜色展示。

2 个答案:

答案 0 :(得分:3)

您可以这样做:

第1步:子类UIPageControl并添加属性dotImages以保存所有点的图像。

class MyPageControl : UIPageControl {
var dotImages : [UIImage?] = []

第2步:初始化dotImages

required init(coder: NSCoder) {
    super.init(coder: coder)!

    let firstDotImage = UIImage.init(named: "dot1.png")
    let secondDotImage = UIImage.init(named: "dot2.png")
    let thirdDotImage = UIImage.init(named: "dot3.png")
    let fourthDotImage = UIImage.init(named: "dot4.png")
    let fifthDotImage = UIImage.init(named: "dot5.png")

    dotImages = [firstDotImage, secondDotImage, thirdDotImage, fourthDotImage, fifthDotImage]
}

第3步:实施updateDots功能以设置您自己的图片

func updateDots () {
        for var index = 0; index < self.subviews.count; index++ {
        let dot = self.subviews[index] as! UIImageView
        dot.image = self.dotImages[index]
    }
}

第4步:根据您的应用需求调用updateDots功能。您可以在加载视图时调用它,也可以在页面更改时调用它。

答案 1 :(得分:2)

currentPageIndicatorTintColor将起作用。 你可能不得不在这里做一些工作。

func scrollViewDidScroll(scrollView: UIScrollView) {
    //If the page is over 50% scrolled change the dot to the new value.
    //If it is less, change it back if the user is holding the page.
}

我建议的是听取滚动视图,抢占页面,而不是等待事实发生。我的Psuedo代码保持抽象,但如果这还不够,我可以根据您的代码更具体地修改答案,如果您提供示例。

因此,您必须是UIScrollViewDelegate