在UIScrollView(Swift 3)中滚动图像时背景颜色淡入淡出效果

时间:2017-06-11 13:52:36

标签: ios swift uiscrollview uicolor

我在UIScrollView中有很多图像,其中我已经计算了图像的平均颜色,但是当我滚动时,我希望背景颜色(平均颜色)从一种颜色淡入下一种颜色通过图像。这就是我想要的(当我在ebay上滚动图像时注意背景颜色效果): https://drive.google.com/file/d/0B3fFlkF0j7y9VDVycTg3aklCbDg/view

目前为止的源文件: https://www.dropbox.com/s/uw874rb8r9ytzrg/TestApp.zip?dl=0

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

  func fade(fromRed: CGFloat,
              fromGreen: CGFloat,
              fromBlue: CGFloat,
              fromAlpha: CGFloat,
              toRed: CGFloat,
              toGreen: CGFloat,
              toBlue: CGFloat,
              toAlpha: CGFloat,
              withPercentage percentage: CGFloat) -> UIColor {

    let red: CGFloat = (toRed - fromRed) * percentage + fromRed
    let green: CGFloat = (toGreen - fromGreen) * percentage + fromGreen
    let blue: CGFloat = (toBlue - fromBlue) * percentage + fromBlue
    let alpha: CGFloat = (toAlpha - fromAlpha) * percentage + fromAlpha

    // return the fade colour
    return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}