Can I use drawRect to refresh a UIView subclass?
- by Timbo
I've created a subclass of UIView called Status which is designed to display a rectangle of a certain size (within a view) depending on the value of a variable.
// Interface
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
@interface Status: UIView {
NSString* name;
int someVariable;
}
@property int someVariable;
@property (assign) NSString *name;
- (void) createStatus: (NSString*)withName;
- (void) drawRect:(CGRect)rect;
@end
// Implementation
#import "Status.h"
@implementation Status
@synthesize name, someVariable;
- (void) createStatus: (NSString*)withName {
name = withName;
someVariable = 10000;
}
- (void) drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
//Draw Status
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1); // fill
CGContextFillRect(context, CGRectMake(0.0, 0.0, someVariable, 40.0));
}
//// myviewcontroller implementation
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
myStatus = [[Status alloc] initWithFrame:CGRectMake(8,8,200,56)];
myStatus.backgroundColor = [UIColor grayColor];
[self.view addSubview:myStatus];
}
How do I set this up so I can repeatedly call a refresh of the status bar? I'll probably call the refresh 4 times per second using a NSTimer, I'm just not sure what to call or if I should move this rectangle drawing to a separate function or something...
Thanks in advance for your help :)