我创建了一个视图,并设置了两个属性backgroundColor
和backgroundAlpha
,当我设置v.background = color
时,backgroundColor为蓝色,但是当我设置v.background = [color colorWithAlphaComponent:b]
时, backgroundColor是清晰的,好像b是零;
+ (instancetype)initWithBgrColor:(UIColor *)color bgrAlpha:(NSInteger) b
{
view *v = [[view alloc] init];
v.backgroundColor = [color colorWithAlphaComponent:b];
return v;
}
答案 0 :(得分:0)
你一定不要看编译器的输出,看Xcode警告你参数类型。根据文档,colorWithAlphaComponent的alpha值参数是介于0.0和1.0之间的CGFloat,因此,您的NSInteger不正确。将您的代码更改为:
+ (instancetype)initWithBgrColor:(UIColor *)color bgrAlpha:(CGFloat) b
{
NSLog(@"initWithBgrColor: b = %f", (float)b);
view *v = [[view alloc] init];
v.backgroundColor = [color colorWithAlphaComponent:b];
return v;
}
https://developer.apple.com/reference/uikit/uicolor?language=objc