即使我以前提过这个问题,我想再次提出来,澄清我想要帮助的内容.我想知道如何在xCode中创建一个背景和iOS应用程序,类似于随着时间的推移而变化的太阳天气应用程序(屏幕截图)的背景.正如你可以看到,渐变很顺利,显然包含两个以上的要点.
任何帮助的例子或代码片段将不胜感激.再次感谢,本.
任何帮助的例子或代码片段将不胜感激.再次感谢,本.
解决方法
我需要的是:
>将CAGradientLayer添加到视图控制器(或自定义视图)中,例如:
@property (nonatomic,retain) CAGradientLayer *gradient;
>在视图控制器中,在viewDidLoad中,创建并添加渐变图层到您的视图:
self.gradient = [CAGradientLayer layer]; gradient.frame = self.view.bounds; gradient.colors = [NSArray arrayWithObjects: (id)topColor.CGColor,(id)bottomColor.CGColor,nil]; gradient.locations = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0f],[NSNumber numberWithFloat:0.7],nil]; [self.view.layer addSublayer:self.gradient];
3A.添加一个NSTimer到您的控制器将通过执行以适当的时间间隔更新self.gradient:
topColor = ...; bottomColor = ...; NSArray* newColors = [NSArray arrayWithObjects: (id)topColor.CGColor,nil]; [(CAGradientLayer *)self.layer setColors:newColors];
这将允许您在每个步骤中准确地确定要用于渐变的颜色.否则,您可以尝试使用动画,如下所示:
3B.添加这样的动画,
- (void)animateLayer... { [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ [CATransaction begin]; [CATransaction setAnimationDuration:duration]; topColor = ...; bottomColor = ...; NSArray* newColors = [NSArray arrayWithObjects: (id)topColor.CGColor,nil]; [(CAGradientLayer *)self.layer setColors:newColors]; [CATransaction commit]; } completion:^(BOOL b) { [self animateLayer..]; }]; }
您也可以组合3a和3b.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。