为了方便使用,这里提供了一个背景图片颜色比较转变比较柔和的方法,之前在网上找的都觉得不够完善后面自己整理了一下,希望能帮助到其他人。有什么不足之处还请多多指教。
backgroundCount = 0;//全局变量 int类型
NSDate *nowDate = [NSDate date];
backgroundTimer = [[NSTimer alloc] initWithFireDate:nowDate interval:0.01 target:self selector:@selector(drawBackgroundFountion) userInfo:nil repeats:YES];//倒计时每0.01秒调用一次
[[NSRunLoop currentRunLoop] addTimer:backgroundTimer forMode:NSRunLoopCommonModes];
-(void)drawBackgroundFountion{
backgroundCount++;//调用一次计数加1
UIColor *testColor = [self getColorWithName:_nowBgImgName];
UIColor *blueColer = [self getColorWithName:_pagesModel.background];
BOOL success = [testColor getHue:&beforeHue saturation:&beforeSaturation brightness:&beforeBrightness alpha:&beforeAlpha];
if (success) {
// NSLog(@"success: hue: %0.2f, saturation: %0.2f, brightness: %0.2f, alpha: %0.2f", beforeHue, beforeSaturation, beforeBrightness, beforeAlpha);
}else{
NSLog(@"failed!!!!");
}
BOOL bluesuccess = [blueColer getHue:&AfterHue saturation:&AfterSaturation brightness:&AfterBrightness alpha:&AfterAlpha];
if (bluesuccess) {
// NSLog(@"success: hue: %0.2f, saturation: %0.2f, brightness: %0.2f, alpha: %0.2f", AfterHue, AfterSaturation, AfterBrightness, AfterAlpha);
}else{
NSLog(@"failed!!!!");
}
//输出结果为:success: hue: 0.10, saturation: 0.79, brightness: 0.53, alpha: 1.00
//2.HSV 转换为 RGB
CGFloat red;
CGFloat green;
CGFloat blue;
red = beforeHue + (AfterHue - beforeHue) * backgroundCount/100.0;
green = beforeSaturation + (AfterSaturation - beforeSaturation) *backgroundCount/100.0;
blue = beforeBrightness + (AfterBrightness - beforeBrightness) *backgroundCount/100.0;
UIColor *testColorh = [UIColor colorWithHue:red saturation:green brightness:blue alpha:1.00];//获取颜色
BOOL success1 = [testColorh getRed:&red green:&green blue:&blue alpha:&AfterAlpha];
if (success1) {
// NSLog(@"success: red: %0.2f,green: %0.2f, blue: %0.2f , alpha: %0.2f",red,green,blue,AfterAlpha);
bgImgView.backgroundColor = testColorh;//给UIImageView的背景颜色赋值
}else{
NSLog(@"failed!!!!");
}
if (backgroundCount == 100) {//调用100次后结束,也就是1秒
[backgroundTimer invalidate];
backgroundTimer = nil;
}
}