Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS CAGradientLayer无法使用某些颜色_Ios_Objective C_Iphone_Xcode - Fatal编程技术网

iOS CAGradientLayer无法使用某些颜色

iOS CAGradientLayer无法使用某些颜色,ios,objective-c,iphone,xcode,Ios,Objective C,Iphone,Xcode,我用下面的代码来设置我的视图的渐变背景色 CAGradientLayer *gradient = [CAGradientLayer layer]; gradient.frame = self.view.bounds; gradient.colors = @[(id)[UIColor colorWithRed:44/255 green:58/255 blue:76/255 alpha:1].CGColor, (id)[UIColor colorWithRed:17/255 gre

我用下面的代码来设置我的视图的渐变背景色

 CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.view.bounds;
    gradient.colors = @[(id)[UIColor colorWithRed:44/255 green:58/255 blue:76/255 alpha:1].CGColor, (id)[UIColor colorWithRed:17/255 green:22/255 blue:30/255 alpha:1].CGColor];
    [self.view.layer insertSublayer:gradient atIndex:0];
但它不起作用。屏幕将以黑色显示。但当我试着像下面这样设置颜色时

gradient.colors = @[(id)[UIColor blueColor].CGColor, (id)[UIColor redColor].CGColor];
然后工作正常。我得到了正确的输出。有谁能帮我弄清楚为什么渐变色不能与UIColor colorWithRed函数一起工作

提前谢谢。

像这样试试吧

 -(CAGradientLayer *)makeGradientView :(UIColor *)topColor BottomColor:(UIColor *)bottomColor withFrame:(CGRect )rect
{
    CAGradientLayer *theViewGradient = [CAGradientLayer layer];
    NSArray *gradientLocations = [NSArray arrayWithObjects:[NSNumber numberWithInt:0.0],[NSNumber numberWithInt:1.0], nil];
    theViewGradient.locations = gradientLocations;
    theViewGradient.colors = [NSArray arrayWithObjects: (id)topColor.CGColor,(id)bottomColor.CGColor,(id)bottomColor.CGColor,(id)bottomColor.CGColor, (id)bottomColor.CGColor, nil];
    theViewGradient.frame = rect;
    theViewGradient.startPoint = CGPointMake(1,1);
    theViewGradient.endPoint = CGPointMake(1.0,0.1);
    return theViewGradient;
}
@property (weak, nonatomic) IBOutlet UIView *mainView;
CAGradientLayer *theViewGradient = [self makeGradientView:[UIColor colorWithRed:189/255.0 green:227/255.0 blue:106/255.0 alpha:0.5] BottomColor:[UIColor colorWithRed:13/255.0 green:184/255.0 blue:196/255.0 alpha:0.5] withFrame:self.view.bounds];
[_mainView.layer addSublayer:theViewGradient];
你可以这样用

 -(CAGradientLayer *)makeGradientView :(UIColor *)topColor BottomColor:(UIColor *)bottomColor withFrame:(CGRect )rect
{
    CAGradientLayer *theViewGradient = [CAGradientLayer layer];
    NSArray *gradientLocations = [NSArray arrayWithObjects:[NSNumber numberWithInt:0.0],[NSNumber numberWithInt:1.0], nil];
    theViewGradient.locations = gradientLocations;
    theViewGradient.colors = [NSArray arrayWithObjects: (id)topColor.CGColor,(id)bottomColor.CGColor,(id)bottomColor.CGColor,(id)bottomColor.CGColor, (id)bottomColor.CGColor, nil];
    theViewGradient.frame = rect;
    theViewGradient.startPoint = CGPointMake(1,1);
    theViewGradient.endPoint = CGPointMake(1.0,0.1);
    return theViewGradient;
}
@property (weak, nonatomic) IBOutlet UIView *mainView;
CAGradientLayer *theViewGradient = [self makeGradientView:[UIColor colorWithRed:189/255.0 green:227/255.0 blue:106/255.0 alpha:0.5] BottomColor:[UIColor colorWithRed:13/255.0 green:184/255.0 blue:196/255.0 alpha:0.5] withFrame:self.view.bounds];
[_mainView.layer addSublayer:theViewGradient];
试试这个

 -(CAGradientLayer *)makeGradientView :(UIColor *)topColor BottomColor:(UIColor *)bottomColor withFrame:(CGRect )rect
{
    CAGradientLayer *theViewGradient = [CAGradientLayer layer];
    NSArray *gradientLocations = [NSArray arrayWithObjects:[NSNumber numberWithInt:0.0],[NSNumber numberWithInt:1.0], nil];
    theViewGradient.locations = gradientLocations;
    theViewGradient.colors = [NSArray arrayWithObjects: (id)topColor.CGColor,(id)bottomColor.CGColor,(id)bottomColor.CGColor,(id)bottomColor.CGColor, (id)bottomColor.CGColor, nil];
    theViewGradient.frame = rect;
    theViewGradient.startPoint = CGPointMake(1,1);
    theViewGradient.endPoint = CGPointMake(1.0,0.1);
    return theViewGradient;
}
@property (weak, nonatomic) IBOutlet UIView *mainView;
CAGradientLayer *theViewGradient = [self makeGradientView:[UIColor colorWithRed:189/255.0 green:227/255.0 blue:106/255.0 alpha:0.5] BottomColor:[UIColor colorWithRed:13/255.0 green:184/255.0 blue:196/255.0 alpha:0.5] withFrame:self.view.bounds];
[_mainView.layer addSublayer:theViewGradient];
你可以这样用

 -(CAGradientLayer *)makeGradientView :(UIColor *)topColor BottomColor:(UIColor *)bottomColor withFrame:(CGRect )rect
{
    CAGradientLayer *theViewGradient = [CAGradientLayer layer];
    NSArray *gradientLocations = [NSArray arrayWithObjects:[NSNumber numberWithInt:0.0],[NSNumber numberWithInt:1.0], nil];
    theViewGradient.locations = gradientLocations;
    theViewGradient.colors = [NSArray arrayWithObjects: (id)topColor.CGColor,(id)bottomColor.CGColor,(id)bottomColor.CGColor,(id)bottomColor.CGColor, (id)bottomColor.CGColor, nil];
    theViewGradient.frame = rect;
    theViewGradient.startPoint = CGPointMake(1,1);
    theViewGradient.endPoint = CGPointMake(1.0,0.1);
    return theViewGradient;
}
@property (weak, nonatomic) IBOutlet UIView *mainView;
CAGradientLayer *theViewGradient = [self makeGradientView:[UIColor colorWithRed:189/255.0 green:227/255.0 blue:106/255.0 alpha:0.5] BottomColor:[UIColor colorWithRed:13/255.0 green:184/255.0 blue:196/255.0 alpha:0.5] withFrame:self.view.bounds];
[_mainView.layer addSublayer:theViewGradient];
试试这个:

CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:202.0/255.0 green:107.0/255.0 blue:122.0/255.0 alpha:1] CGColor], (id)[[UIColor colorWithRed:192.0/255.0 green:144.0/255.0 blue:120.0/255.0 alpha:1] CGColor], nil];
gradient.frame = self.view.bounds;
[self.view.layer insertSublayer:gradient atIndex:0];
试试这个:

CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:202.0/255.0 green:107.0/255.0 blue:122.0/255.0 alpha:1] CGColor], (id)[[UIColor colorWithRed:192.0/255.0 green:144.0/255.0 blue:120.0/255.0 alpha:1] CGColor], nil];
gradient.frame = self.view.bounds;
[self.view.layer insertSublayer:gradient atIndex:0];

为了使原始海报的代码起作用,他只需将每个颜色分量除以255.0,而不是255,否则结果将被截断为整数

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = @[(id)[UIColor colorWithRed:44/255.0 green:58/255.0 blue:76/255.0 alpha:1].CGColor, (id)[UIColor colorWithRed:17/255.0 green:22/255.0 blue:30/255.0 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];
或者,将股息设为浮动值:

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = @[(id)[UIColor colorWithRed:44.0/255 green:58.0/255 blue:76.0/255 alpha:1].CGColor, (id)[UIColor colorWithRed:17.0/255 green:22.0/255 blue:30.0/255 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = @[(id)[UIColor colorWithRed:44.0/255.0 green:58.0/255.0 blue:76.0/255.0 alpha:1].CGColor, (id)[UIColor colorWithRed:17.0/255.0 green:22.0/255.0 blue:30.0/255.0 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];
或者,最简单的解决方案是,将除法中的两个数字都设为浮点值:

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = @[(id)[UIColor colorWithRed:44.0/255 green:58.0/255 blue:76.0/255 alpha:1].CGColor, (id)[UIColor colorWithRed:17.0/255 green:22.0/255 blue:30.0/255 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = @[(id)[UIColor colorWithRed:44.0/255.0 green:58.0/255.0 blue:76.0/255.0 alpha:1].CGColor, (id)[UIColor colorWithRed:17.0/255.0 green:22.0/255.0 blue:30.0/255.0 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];

为了使原始海报的代码起作用,他只需将每个颜色分量除以255.0,而不是255,否则结果将被截断为整数

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = @[(id)[UIColor colorWithRed:44/255.0 green:58/255.0 blue:76/255.0 alpha:1].CGColor, (id)[UIColor colorWithRed:17/255.0 green:22/255.0 blue:30/255.0 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];
或者,将股息设为浮动值:

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = @[(id)[UIColor colorWithRed:44.0/255 green:58.0/255 blue:76.0/255 alpha:1].CGColor, (id)[UIColor colorWithRed:17.0/255 green:22.0/255 blue:30.0/255 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = @[(id)[UIColor colorWithRed:44.0/255.0 green:58.0/255.0 blue:76.0/255.0 alpha:1].CGColor, (id)[UIColor colorWithRed:17.0/255.0 green:22.0/255.0 blue:30.0/255.0 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];
或者,最简单的解决方案是,将除法中的两个数字都设为浮点值:

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = @[(id)[UIColor colorWithRed:44.0/255 green:58.0/255 blue:76.0/255 alpha:1].CGColor, (id)[UIColor colorWithRed:17.0/255 green:22.0/255 blue:30.0/255 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = @[(id)[UIColor colorWithRed:44.0/255.0 green:58.0/255.0 blue:76.0/255.0 alpha:1].CGColor, (id)[UIColor colorWithRed:17.0/255.0 green:22.0/255.0 blue:30.0/255.0 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];