【问题标题】:How do I change the background color of UIScrollView in Swift?如何在 Swift 中更改 UIScrollView 的背景颜色?
【发布时间】:2020-01-26 03:42:44
【问题描述】:

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

数组:

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]

然后是scrollview内容

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)
    }
}

滚动视图背景颜色变为紫色,但不会因页面而异。改变颜色的正确方法是什么?

【问题讨论】:

    标签: ios swift uiscrollview


    【解决方案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

    【讨论】:

    • 我实际上是从 PageView 开始的,但它不适用于这个项目,因为我正在集成 Lottie。上面的代码永远不会被调用,所以我无法确定它是否有效。
    • 抱歉,请尝试将 scrollViewDidScroll(scrollView: UIScrollView!) 替换为 scrollViewDidScroll(_ scrollView: UIScrollView)。假设您已将滚动视图的委托设置为您的视图控制器(您似乎拥有)并将您的视图控制器扩展为 UIScrollViewDelegate 并实现了此委托方法,则每次滚动视图“滚动”时都应调用它。
    • 这有所不同。谢谢。
    • 没问题。你介意在某个时候告诉我为什么 Lottie 不能与 UIPageViewController 一起工作吗?我刚刚制作了一个类似 tinder 的应用程序,它利用了一个横向滚动的 UIPageViewController,我觉得我可以提供帮助。或者至少分享一些有用的代码。
    • 很高兴。我将在 LinkedIn 上连接。
    猜你喜欢
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 2015-11-18
    • 2017-07-06
    • 2015-05-30
    • 1970-01-01
    • 2018-01-01
    • 2018-10-25
    相关资源
    最近更新 更多