我已經創建了一個名爲Status的UIView的子類,它被設計爲根據變量的值顯示一定大小的矩形(在視圖內)。我可以使用drawRect刷新UIView子類嗎?
// 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];
}
我該如何設置,以便我可以反覆調用狀態欄的刷新?我可能會使用NSTimer每秒鐘刷新4次,我只是不確定要調用什麼,或者如果我應該將這個矩形繪圖移動到單獨的函數或其他東西...
在此先感謝幫助:)
太容易了,這是完美的:)。 – Timbo 2010-06-19 23:51:42