基本的に色の付いた単一のボタンを表すColorButtonと、ColorButtonオブジェクトのグリッドであるPaletteViewがあります。UIColorプリセット値を渡す際の問題
ColorButton.h
@interface ColorButton : UIButton {
UIColor* color;
}
-(id) initWithFrame:(CGRect)frame andColor:(UIColor*)color;
@property (nonatomic, retain) UIColor* color;
@end
ColorButton.m
@implementation ColorButton
@synthesize color;
- (id)initWithFrame:(CGRect)frame andColor:(UIColor*)aColor{
self = [super initWithFrame:frame];
if (self) {
self.color = aColor;
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
const float* colors = CGColorGetComponents(color.CGColor);
CGContextSetRGBFillColor(context, colors[0], colors[1], colors[2], colors[3]);
CGContextFillRect(context, rect);
}
PaletteView.m
0123:のコードは次のようになります
- (void) initPalette {
ColorButton* cb = [[ColorButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30) andColor:[UIColor grayColor]];
[self addSubview:cb];
}
問題はそれが機能しないことです。何も表示されていません。ただし、次のコードは動作します。この場合
PaletteView.m
- (void) initPalette {
UIColor *color = [[UIColor alloc]
initWithRed: (float) (100/255.0f)
green: (float) (100/255.0f)
blue: (float) (1/255.0f)
alpha: 1.0];
ColorButton* cb = [[ColorButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30) andColor:color];
[self addSubview:cb];
}
Iは[UIColor grayColor]と比較して、UIColorオブジェクトを自動解放しない通過 - 自動解放オブジェクト。
また、コードを以下を動作します。
ColorButton.m
- (id)initWithFrame:(CGRect)frame andColor:(UIColor*)aColor{
self = [super initWithFrame:frame];
if (self) {
//self.color = aColor;
self.color = [UIColor redColor];
}
return self;
}
誰かが私は[UIColor grayColor]のようなオブジェクトを渡すことはできませんなぜ、ここで何が起こっているか説明できますか?そして私の仕事を解決する正しい方法は何ですか?PaletteViewからColorButtonにカラー値を渡しますか?
ありがとうございます!
ビンゴ!このAPIは動作します!私に色空間のものを指摘してくれてありがとう。 – lstipakov