我使用水平UIScrollView,我希望根据内容偏移的x值进行背景颜色转换。
示例: UIScrollView的宽度为640px。当内容偏移量等于0px时,背景颜色必须为红色。当内容偏移量为320像素时,背景必须为黄色。但最重要的是,当UIScrollview介于0px和320px之间时,背景颜色必须介于红色和黄色之间。
提示:当您从搜索向左滑动时,iOS的Twitter应用程序具有相同的动画。导航上的标签稍微消失。
答案 0 :(得分:15)
您需要根据偏移百分比创建颜色。
在这样的颜色之间创建切换的最简单方法是使用HSB颜色空间。
此外,这不是一个动画。 "动画"效果由scrollview提供给你。您只需在每次滚动视图更改时设置颜色。
在委托方法中你可以这样做。
编辑以更灵活
// this just calculates the percentages now and passes it off to another method.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// vertical
CGFloat maximumVerticalOffset = scrollView.contentSize.height - CGRectGetHeight(scrollView.frame);
CGFloat currentVerticalOffset = scrollView.contentOffset.y;
// horizontal
CGFloat maximumHorizontalOffset = scrollView.contentSize.width - CGRectGetWidth(scrollView.frame);
CGFloat currentHorizontalOffset = scrollView.contentOffset.x;
// percentages
CGFloat percentageHorizontalOffset = currentHorizontalOffset / maximumHorizontalOffset;
CGFloat percentageVerticalOffset = currentVerticalOffset / maximumVerticalOffset;
[self scrollView:scrollView didScrollToPercentageOffset:CGPointMake(percentageHorizontalOffset, percentageVerticalOffset)];
}
// this just gets the percentage offset.
// 0,0 = no scroll
// 1,1 = maximum scroll
- (void)scrollView:(UIScrollView *)scrollView didScrollToPercentageOffset:(CGPoint)percentageOffset
{
UIColor *HSBColor = [self HSBColorForOffsetPercentage:percentageOffset.x];
UIColor *RGBColor = [self RGBColorForOffsetPercentage:percentageOffset.x];
}
// HSB color just using Hue
- (UIColor *)HSBColorForOffsetPercentage:(CGFloat)percentage
{
CGFloat minColorHue = 0.0;
CGFloat maxColorHue = 0.2; // this is a guess for the yellow hue.
CGFloat actualHue = (maxColorHue - minColorHue) * percentage + minColorHue;
// change these values to get the colours you want.
// I find reducing the saturation to 0.8 ish gives nicer colours.
return [UIColor colorWithHue:actualHue saturation:1.0 brightness:1.0 alpha:1.0];
}
// RGB color using all R, G, B values
- (UIColor *)RGBColorForOffsetPercentage:(CGFloat)percentage
{
// RGB 1, 0, 0 = red
CGFloat minColorRed = 1.0;
CGFloat minColorGreen = 0.0;
CGFloat minColorBlue = 0.0;
// RGB 1, 1, 0 = yellow
CGFloat maxColorRed = 1.0;
CGFloat maxColorGreen = 1.0;
CGFloat maxColorBlue = 0.0;
// if you have specific beginning and end RGB values then set these to min and max respectively.
// it should even work if the min value is greater than the max value.
CGFloat actualRed = (maxColorRed - minColorRed) * percentage + minColorRed;
CGFloat actualGreen = (maxColorGreen - minColorGreen) * percentage + minColorGreen;
CGFloat actualBlue = (maxColorBlue - minColorBlue) * percentage + minColorBlue;
return [UIColor colorWithRed:actualRed green:actualGreen blue:actualBlue alpha:1.0];
}
我不知道RGB方法将如何对中间值执行。它可能在中间变成棕色......但你可以玩。
这可以让您了解如何使用滚动视图设置 ANYTHING 的动画作为控制它的方式。
使用此方法可以控制不透明度,大小,旋转,字体大小等...您甚至可以组合多个内容(就像我使用RGB一样)。
答案 1 :(得分:0)
受@Fogmeister的回答启发,我迅速将各个部分放在一起。
extension UIColor {
static func color(between start: UIColor, and end: UIColor, percentage: CGFloat) -> UIColor {
let startRGB = start.rgba
let stopRGB = end.rgba
let red: CGFloat = (stopRGB.red - startRGB.red) * percentage + startRGB.red
let green: CGFloat = (stopRGB.green - startRGB.green) * percentage + startRGB.green
let blue: CGFloat = (stopRGB.blue - startRGB.blue) * percentage + startRGB.blue
let alpha: CGFloat = (stopRGB.alpha - startRGB.alpha) * percentage + startRGB.alpha
return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
var rgba: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
getRed(&red, green: &green, blue: &blue, alpha: &alpha)
return (red, green, blue, alpha)
}
}
现在您可以执行以下操作:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offset = scrollView.contentOffset.y
let scrolledDistance = offset + scrollView.contentInset.top
let progress = max(0, min(1, scrolledDistance / scrollView.contentSize.height))
someLabel.textColor = .color(between: .red, and: .green, percentage: progress)
}
警告::此实施将触发swiftlints large_tuple 规则。但是您可以禁用,只需在 disabled_rules 部分中添加'-large_tuple'。
答案 2 :(得分:-3)
在你的scrollView委托中尝试这样的事情:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGPoint a = self.mainScrollView.contentOffset;
if(a.x >= 320){
[UIView animateWithDuration:0.5 animations:^{
[self.view setBackgroundColor:[UIColor redColor]];
}];
}
else{
[UIView animateWithDuration:0.5 animations:^{
[self.view setBackgroundColor:[UIColor yellowColor]];
}];
}
}