基于uicolor的渐变不起作用

uicolor based gradient not working

我正在尝试使用我创建的一些自定义 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];

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

From the documentation, the colors property is:

An array of CGColorRef objects defining the color of each gradient stop. Animatable.

因此你想要:

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