【问题标题】:uicolor based gradient not working基于uicolor的渐变不起作用
【发布时间】:2016-01-25 22:55:33
【问题描述】:

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

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

【问题讨论】:

  • 你真的想使用 Objective-C 集合字面量:而不是 [NSArray arrayWithObjects: this, that, nil],使用 @[this, that]

标签: ios objective-c uicolor cagradientlayer


【解决方案1】:

From the documentation, the colors property is:

定义每个渐变色标的 CGColorRef 对象数组。动画。

所以你想要:

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

【讨论】:

    猜你喜欢
    • 2018-12-12
    • 1970-01-01
    • 2016-12-23
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 2011-08-25
    相关资源
    最近更新 更多