我已经创建了一个名为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