【问题标题】:Background Colour Continuous Change - iOS背景颜色连续变化 - iOS
【发布时间】:2016-11-18 15:59:38
【问题描述】:

如何使UIView的背景颜色像Instagram的登录屏幕一样连续变化,如图所示,内存中没有加载。

【问题讨论】:

标签: ios objective-c uiview colors background-color


【解决方案1】:

要改变你刚刚做的背景颜色:

    self.view.backgroundColor=yourColor;

要连续更改,您可以使用计时器。如果您有一个数组颜色作为背景,请将颜色存储在一个数组中。

   NSArray *colorArray=@[colo1,color2....colorn];

您还可以生成random 颜色并将其传递给视图背景。在这种情况下,您可以这样做(在这种情况下,您不必将颜色存储在数组中)

      self.view.backgroundColor=metodThatReturnsRandomColor.

或者,声明每 1.0 秒调用 changeColor 方法的计时器。

  @property (nonatomic, retain) NSTimer * timerIvar;


 self.timerIvar  = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeColor) userInfo:nil repeats:YES];

方法是:

-(void)changeColor{
         if(x<colorArray.count){
        self.view.backgroundColor=colorArray[x];
        }else{
          x=0;
       }
        x++;
  }

同样的逻辑也适用于图片/gradient colors

并记住 viewWillDisappear

- (void)viewWillDisappear:(BOOL)animated
{
  ...
  [self.timerIvar invalidate];
 self.timerIvar = nil;
}

【讨论】:

  • 好吧,这看起来有点糟糕,否则您将不得不拥有一个包含所有颜色渐变的非常庞大的阵列
  • 最好在循环中创建颜色渐变,而不是从数组中获取它。否则它会看起来像一个廉价的霓虹灯,会改变颜色。
  • 我不认为存储颜色会产生内存问题,只要您管理得当。 @s1ddok
  • 我同意@NSNoob。除非 OP 对颜色有特殊要求,否则他可以循环创建颜色。如果他有一组特定的兴趣,那么将它们存储在一个数组中将是更简单的解决方案。
猜你喜欢
  • 2017-04-14
  • 2021-10-02
  • 2014-09-14
  • 2021-02-13
  • 2017-12-10
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
相关资源
最近更新 更多