在视图中确实加载方法所有数组加载不同颜色的浮点数。
- (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;
}
这个功能完美无缺。但我的问题是它的颜色变化快,所以我想慢慢改变颜色。
如何使用慢动画更改图层中的渐变颜色?