示例:UIScrollVIEw的宽度为640px.当内容偏移量等于0px时,背景颜色必须为红色.当内容偏移量为320像素时,背景必须为黄色.但最重要的是,当UIScrollvIEw介于0px和320px之间时,背景颜色必须介于红色和黄色之间.
提示:当您从搜索向左滑动时,iOS的Twitter应用程序具有相同的动画.导航上的标签稍微消失.
解决方法 您需要根据偏移百分比创建颜色.在这样的颜色之间创建切换的最简单方法是使用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 = 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一样).
总结以上是内存溢出为你收集整理的ios – UIScrollview动画取决于内容偏移量全部内容,希望文章能够帮你解决ios – UIScrollview动画取决于内容偏移量所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)