如何在Swift中更改UIScrollView的背景颜色?

问题描述 投票:0回答:1

我正在运行Xcode 11,并且希望能够在6页设置中的UIScrollView在页面之间移动时更改其背景颜色。我已经能够更改每个页面的标签内容,但是背景颜色“跳到”了颜色数组中的最后一个。

数组:

let stringArray = ["<- Swipe to begin", "Welcome", "Info", "more info", "more info 2", "end"]

let backgroundColorArray = [UIColor.orange, UIColor.blue, UIColor.red, UIColor.white, UIColor.green, UIColor.purple]

然后滚动视图内容

func setupScrollView() {
    scrollView.delegate = self
    scrollView.contentSize = CGSize(width: self.view.frame.size.width * 6, height: scrollView.frame.size.height)

    for k in 0 ... 5 {
        scrollView.backgroundColor = backgroundColorArray[k]
    }

    for i in 0 ... 5 {
        let label = UILabel(frame: CGRect(x: scrollView.center.x + CGFloat(i) * self.view.frame.size.width - 130, y: 50, width: 260, height: 30))
        label.font = UIFont.boldSystemFont(ofSize: 26)
        label.textAlignment = .center
        label.text = stringArray[i]

        scrollView.addSubview(label)
    }
}

scrollview背景颜色变为紫色,但在页面之间没有变化。改变颜色的正确方法是什么?

ios swift uiscrollview
1个回答
1
投票

通话时

for k in 0 ... 5 {
    scrollView.backgroundColor = backgroundColorArray[k]
}

您正在遍历backgroundColorArray中的每种颜色并更新值。由于.purplebackgroundColorArray中的最后一个元素,因此在此for循环末尾的滚动视图被设置为紫色。您的for循环基本上与scrollView.backgroundColor = backgroundColorArray[5]做相同的事情。

现在,我建议您做的是实现UIScrollViewDelegate方法。您可以实现scrollViewDidScroll(scrollView:)。每次滚动视图的内容偏移量更改时都会调用此方法。然后,您可以做的是设置一个switch语句,该语句检查滚动视图的内容偏移量,并相应地分配颜色:

extension MyViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //Assuming your pages are situated horizontally from one another
        let page = scrollView.contentOffset.x / view.frame.width
        if page < 1 {
            scrollView.backgroundColor = backgroundColorArray[0]
        } else if page > 1 && page < 2 {
            scrollView.backgroundColor = backgroundColorArray[1]
        } else if page > 2 && page < 3 {
            scrollView.backgroundColor = backgroundColorArray[2]
        } else if page > 3 && page < 4 {
            scrollView.backgroundColor = backgroundColorArray[3]
        } else if page > 4 && page < 5 {
            scrollView.backgroundColor = backgroundColorArray[4]
        } else if page > 5 && page < 6 {
            scrollView.backgroundColor = backgroundColorArray[5]
        }
    }
}

您还可以根据滚动视图的内容偏移量在颜色之间进行interpolate。这是我用来执行此操作的UIColor扩展名:

extension UIColor {
    static func interpolate(from fromColor: UIColor, to toColor: UIColor, with progress: CGFloat) -> UIColor {
        let fromComponents = fromColor.components
        let toComponents = toColor.components

        let r = (1 - progress) * fromComponents.r + progress * toComponents.r
        let g = (1 - progress) * fromComponents.g + progress * toComponents.g
        let b = (1 - progress) * fromComponents.b + progress * toComponents.b
        let a = (1 - progress) * fromComponents.a + progress * toComponents.a

        return UIColor(red: r, green: g, blue: b, alpha: a)
    }
}

但是我强烈建议您看看UIPageViewController。听起来它可以做更多您尝试手动完成的事情。看看我最近问过的这个问题:Pan (not swipe) between view controllers

© www.soinside.com 2019 - 2024. All rights reserved.