gpt4 book ai didi

ios - CAShapeLayer 描边颜色不读取十六进制颜色代码转换

转载 作者:行者123 更新时间:2023-11-29 01:51:22 24 4
gpt4 key购买 nike

我正在使用 UIPickerView,它从具有十六进制颜色代码列表的数组中读取,然后用于用所有不同的颜色填充选择器 View 的每一行。我正在使用一种将十六进制代码转换为 RGB 代码的方法,以及一个 UIlabel 来更改每一行的颜色。我的用于更改 UIPickerView 每一行中的 UILabel 颜色的 for 循环工作得很好,但是当我尝试使用相同的十六进制代码转换执行相同的颜色更改时,我正在使用 CAShapeLayer 在 View 中绘制圆的笔触颜色, 什么都没发生 。似乎 UIColor 不接受十六进制代码转换,尽管 UIPickerView 中每一行的 UILabel 的背景都可以。任何人都知道为什么这不起作用?

这是为 UIPickerView 中的每一行更改 UILabel 的背景颜色的 for 循环:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

UILabel *pickerLabel = [[UILabel alloc]init];

for (int currentIndex=0; currentIndex<[self.colorArray count]; currentIndex++) {

[pickerLabel setBackgroundColor: [self colorWithHexString:[self.colorArray objectAtIndex:row]]];

}

return pickerLabel;
}

这是我正在尝试更改正在使用的 CAShapeLayer 的笔触颜色的 UIPickerView 委托(delegate)方法:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row   inComponent:(NSInteger)component{

NSString *rowColorHex = [self.colorArray objectAtIndex:[self.picker selectedRowInComponent:0]];

[self.circleLayer setStrokeColor: [self colorWithHexString: rowColorHex];

//NSLog(@"%@", rowColorHex);
}

这是我创建圆形的方法。请注意,我省略了初始笔触颜色。我可以在我的问题所在的 UIPickerView 的委托(delegate)方法中以相同的方式分配笔触颜色,并且效果很好:
CAShapeLayer *circleLayer = [CAShapeLayer layer];
circleLayer.lineWidth = 50;
circleLayer.fillColor = [[UIColor clearColor] CGColor];
//circleLayer.strokeColor = [[UIColor redColor] CGColor];
[self.imageToBeCropped.layer addSublayer:circleLayer];
self.circleLayer = circleLayer;

最后,这里是十六进制转换方法:
-(UIColor*)colorWithHexString:(NSString*)hex{
NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

// String should be 6 or 8 characters
if ([cString length] < 6) return [UIColor grayColor];

// strip 0X if it appears
if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];

if ([cString length] != 6) return [UIColor grayColor];

// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [cString substringWithRange:range];

range.location = 2;
NSString *gString = [cString substringWithRange:range];

range.location = 4;
NSString *bString = [cString substringWithRange:range];

// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];

return [UIColor colorWithRed:((float) r / 255.0f)
green:((float) g / 255.0f)
blue:((float) b / 255.0f)
alpha:1.0f];
}

最佳答案

Core Animation 层不接受 UIKit 类型——你需要使用 Core Graphics 等价物,在这种情况下是 CGColorRef .您在已经创建它的代码中执行此操作,但是您的 -colorWithHexString:方法返回 UIColor .

[self.circleLayer setStrokeColor:[[self colorWithHexString: rowColorHex] CGColor]];

关于ios - CAShapeLayer 描边颜色不读取十六进制颜色代码转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31372046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com