2

このコードで単純な長方形を描画するUIViewサブクラスがあります。

- (void)drawRect:(CGRect)rect {

//Get the CGContext from this view
CGContextRef context = UIGraphicsGetCurrentContext();

CGColorRef myColor = [UIColor colorWithHue:0 saturation:1 brightness:0.61 alpha:1].CGColor;
//Draw a rectangle
CGContextSetFillColorWithColor(context, myColor);
//Define a rectangle
CGContextAddRect(context, CGRectMake(0, 0, 95.0, 110.0));
//Draw it
CGContextFillPath(context);

}

次に、UISliderを含む別のUIViewControllerがあります

-(IBAction) sliderChanged:(id) sender{

UISlider *slider = (UISlider *)sender;
int sliderValue = (int)[slider value];
float sliderFloat = (float) sliderValue;

NSLog(@"sliderValue ... %d",sliderValue);
NSLog(@"sliderFloat ... %.1f",sliderFloat / 100);

}

ここで、sliderChangedで、UIViewサブクラスに描画されている長方形の背景色を動的に変更できるようにしたいと思います。これをどのように実装すればよいですか?

ありがとう!

4

1 に答える 1

2

UIColor(またはCGColor)値を保持するUIView-Subclassのプロパティを作成します。

ヘッダー内:

@interface MySub : UIView {
NSColor* rectColor;
}

@property (retain) NSColor* rectColor;
@end

実装ファイル:

@implementation MySub
@synthesize rectColor
@end

myViewInstance.rectColor=SomeNSColor;で色を設定できるようになりました。

色を設定した後、新しい背景色で四角形を描画するには、ビューを再描画する必要があります。

[myViewInstance setNeedsDisplay];
于 2010-12-08T21:38:59.733 に答える