Ios 如何为UIview缓慢更改背景层渐变颜色?

Ios 如何为UIview缓慢更改背景层渐变颜色?,ios,ios7,uiview,uiviewanimationtransition,Ios,Ios7,Uiview,Uiviewanimationtransition,在视图中,使用不同颜色的浮点加载所有数组 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. backgroundLayerOne = [CAGradientLayer gradientLayerTwo]; backgroundLayerOne.frame = self.view.bounds; [self.vie

在视图中,使用不同颜色的浮点加载所有数组

 - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

backgroundLayerOne = [CAGradientLayer gradientLayerTwo];
backgroundLayerOne.frame = self.view.bounds;
[self.view.layer insertSublayer:backgroundLayerOne atIndex:0];
nsm_TopX = [[NSMutableArray alloc]initWithObjects:@"0.17",@"0.36",@"0.93",@"0.93",nil];
nsm_BottomX = [[NSMutableArray alloc]initWithObjects:@"0.55",@"0.64",@"0.99",@"0.94",nil];
nsm_TopY = [[NSMutableArray alloc]initWithObjects:@"0.62",@"0.63",@"0.50",@"0.10",nil];
nsm_BottomY = [[NSMutableArray alloc]initWithObjects:@"0.91",@"0.79",@"0.73",@"0.51",nil];
nsm_TopZ = [[NSMutableArray alloc]initWithObjects:@"0.64",@"0.54",@"0.07",@"0.38",nil];
nsm_BottomZ = [[NSMutableArray alloc]initWithObjects:@"0.92",@"0.52",@"0.47",@"0.65",nil];
count=0;



[self setUpView];



 }
设置视图函数调用nstimer以更改颜色

 -(void)setUpView
{
 [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(changeBackgroundWithLayer) userInfo:nil repeats:YES];
}
此功能用于更改背景层

        - (void)changeBackgroundWithLayer
       {
         [backgroundLayerOne removeFromSuperlayer];
          backgroundLayerOne = nil;
          backgroundLayerOne = [self gradientLayer];
          backgroundLayerOne.frame = self.view.bounds;
          [self.view.layer insertSublayer:backgroundLayerOne atIndex:0];
      }
此函数为图层生成渐变颜色

        - (CAGradientLayer *)gradientLayer
          {

            float topX = [[nsm_TopX objectAtIndex:count]floatValue];
            float topY = [[nsm_TopY objectAtIndex:count]floatValue];
            float topZ = [[nsm_TopZ objectAtIndex:count]floatValue];

            float bottomX = [[nsm_BottomX objectAtIndex:count]floatValue];
            float bottomY = [[nsm_BottomY objectAtIndex:count]floatValue];
            float bottomZ = [[nsm_BottomZ objectAtIndex:count]floatValue];




            UIColor *topColor = [UIColor colorWithRed:topX green:topY blue:topZ alpha:1];
            UIColor *bottomColor = [UIColor colorWithRed:bottomX green:bottomY blue:bottomZ alpha:1];

            NSArray *gradientColors = [NSArray arrayWithObjects:(id)topColor.CGColor, (id)bottomColor.CGColor, nil];
            NSArray *gradientLocations = [NSArray arrayWithObjects:[NSNumber numberWithInt:0.0],[NSNumber numberWithInt:1.0], nil];

            CAGradientLayer *gradientLayer = [CAGradientLayer layer];
            gradientLayer.colors = gradientColors;
            gradientLayer.locations = gradientLocations;


if(count < [nsm_TopX count]-1)
{
    count++;
}
else
{
    count =0 ;
}

return gradientLayer;
    }
-(CAGradientLayer*)渐变层
{
float-topX=[[nsm_-topX-objectAtIndex:count]floatValue];
float topY=[[nsm_topY objectAtIndex:count]floatValue];
float-topZ=[[nsm_-topZ-objectAtIndex:count]floatValue];
float bottomX=[[nsm_bottomX objectAtIndex:count]floatValue];
float bottomY=[[nsm_bottomY objectAtIndex:count]floatValue];
float bottomZ=[[nsm_bottomZ objectAtIndex:count]floatValue];
UIColor*topColor=[UIColor COLOR WITHRED:topX green:topY blue:topZ alpha:1];
UIColor*bottomColor=[UIColor colorWithRed:bottomX green:bottomY blue:bottomZ alpha:1];
NSArray*GradientColor=[NSArray arrayWithObjects:(id)topColor.CGColor,(id)bottomColor.CGColor,nil];
NSArray*gradientLocations=[NSArray阵列及其对象:[NSNumber numberWithInt:0.0],[NSNumber numberWithInt:1.0],无];
CAGradientLayer*梯度层=[CAGradientLayer层];
gradientLayer.colors=渐变颜色;
gradientLayer.locations=gradientLocations;
如果(计数<[nsm\U TopX计数]-1)
{
计数++;
}
其他的
{
计数=0;
}
返回梯度层;
}
这个函数工作得很好。但我的问题是它的工作快速颜色变化,所以我想改变颜色缓慢


如何使用慢速动画更改图层中的渐变颜色?

您具体想达到什么效果?通常情况下,
cabasicanitation
应该是有帮助的。您是否尝试使用
UIView
动画块来
insertSubLayer
?是的,但是没有效果,您知道吗?另一个简单的方法@Neverhopess