我有一个自定义表格 View 标题,当用户向下滚动时它会变得模糊。我想更改其中一个导航栏项目的色调颜色(它不是导航栏按钮项目,只是一个 UIButton)。我一直在使用 tableviews contentOffset.y 属性来设置按钮动画,并模糊背景 ImageView 。我也需要对色调颜色执行此操作,但我知道如何操作。
例如,这是使用 alpha 属性缩小个人资料图片和隐藏某些 View 的代码
func movingUp(offset: CGFloat) {
if infoView.alpha >= 0 {
let alphaOffset = (offset - 133)/105.0
infoView.alpha = alphaOffset*1.2
profilePic.alpha = alphaOffset
}
if profileWidth.constant >= 45 {
profilePic.layer.cornerRadius = (((offset - 133)/105.0) * 90)/2
profileHeight.constant = ((offset - 133)/105.0) * 90
profileWidth.constant = ((offset - 133)/105.0) * 90
}
}
最佳答案
我有一个答案可以帮助您... http://www.oliverfoggin.com/controlling-animations-with-a-uiscrollview/
想法是,您可以通过使用当前内容偏移量并将其用作基于最大偏移量的百分比来在两个极值之间移动。
在您的情况下,这很容易,因为颜色是百分比的简单反函数。
即
let color = UIColor(white: 1.0 - percentage, alpha: 1.0)
当百分比为 0.0(即表格顶部)时,白色值为 1.0 = 白色。
当百分比为 1.0(即表格底部)时,白色值为 0.0 = 黑色。
我刚刚意识到我的帖子是在 Objective-C 中。转换为 Swift 应该很容易,因为它只使用简单的数学函数。如果您希望我转换它,请告诉我,我会把它放在这里。
关于ios - 滚动时更改 UIButton 色调颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39877702/