I have a UIView subclass that draws a simple rectangle with this code:
- (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);
}
then, I have a separate UIViewController that has a UISlider in it
-(IBAction) sliderChanged:(id) sender{
UISlider *slider = (UISlider *)sender;
int sliderValue = (int)[slider value];
float sliderFloat = (float) sliderValue;
NSLog(@"sliderValue ... %d",sliderValue);
NSLog(@"sliderFloat ... %.1开发者_Go百科f",sliderFloat / 100);
}
here, in sliderChanged, I would like to be able to dynamically change the background color of the rectangle being drawn in the UIView subclass. how should i go about implementing this ?
thank you!
Create a property of your UIView-Subclass which holds a UIColor (or CGColor) value:
In the header:
@interface MySub : UIView {
NSColor* rectColor;
}
@property (retain) NSColor* rectColor;
@end
In the implementation file:
@implementation MySub
@synthesize rectColor
@end
You can now set the color with myViewInstance.rectColor = SomeNSColor;
After you've set the color, you have to redraw the view in order to draw the rect with the new background color:
[myViewInstance setNeedsDisplay];
精彩评论