Can I use drawRect to refresh a UIView subclass?

Posted by Timbo on Stack Overflow See other posts from Stack Overflow or by Timbo
Published on 2010-06-18T12:11:15Z Indexed on 2010/06/18 12:43 UTC
Read the original article Hit count: 236

Filed under:

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 :)

© Stack Overflow or respective owner

Related posts about objective-c