基于颜色的渐变不起作用

问题描述:

我正在尝试使用某些自定义创建的渐变UIcolors我创建的,但是当我运行该应用程序时,所有显示的都是白色。我创建了应用程序委托的颜色,所以我可以通过使用出来的应用程序,这是我用他们的代码:基于颜色的渐变不起作用

//AppDelegate.h 

#import <UIKit/UIKit.h> 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 
+ (UIColor*)blueColor1; 
+ (UIColor*)blueColor2; 
+ (UIColor*)yellowColor1; 

@property (strong, nonatomic) UIWindow *window; 

@end 

//AppDelegate.m 

+ (UIColor*)blueColor1 { 
    return [UIColor colorWithRed:112.0/255.0 green:184.0/255.0 blue:255.0/255.0 alpha:1.0]; 
} 
+ (UIColor*)blueColor2 { 
    return [UIColor colorWithRed:190.0/255.0 green:243.0/255.0 blue:250.0/255.0 alpha:1.0]; 
} 
+ (UIColor*)yellowColor1 { 
    return [UIColor colorWithRed:245.0/255.0 green:243.0/255.0 blue:204.0/255.0 alpha:1.0]; 
} 

而对于梯度本身我用这个:

CAGradientLayer *gradient = [CAGradientLayer layer]; 
    gradient.frame = self.view.bounds; 
    gradient.colors = [NSArray arrayWithObjects:(id)[AppDelegate blueColor1], (id)[AppDelegate blueColor2], (id)[AppDelegate yellowColor1], nil]; 
    [self.view.layer insertSublayer:gradient atIndex:0]; 

什么我需要做些什么才能让渐变显示?

+0

你真的想使用Objective-C的收集文字:不是'[NSArray的arrayWithObjects:这个,那个,零]',使用'@ [这个,那个] '。 – jcaron

+0

[CAGradientLayer not working]可能重复(http://stackoverflow.com/questions/33060648/cagradientlayer-not-working) – Larme

From the documentation, the colors property is:

限定每个渐变停止的颜色CGColorRef对象的数组。动画。

因此你想:

gradient.colors = @[(id)[AppDelegate blueColor1].CGColor, (id)[AppDelegate blueColor2].CGColor, (id)[AppDelegate yellowColor1].CGColor];