Hallo,
ich möchte in einem Statistik View einen Balken zeichnen, der je nach Erfüllungsgrad zu x Prozent gefüllt ist.
Das Füllen sollte animiert werden.
Was ich bisher getan habe, funktioniert leider nicht. Ist mein Ansatz falsch?
In ViewDidLoad habe ich den subView angelegt und drawRect zeichnet auch das Rechteck.
In ViewDidAppear wollte ich eine Animation starten aber so scheint das nicht zu funktionieren.
Der Wert wird zugewiesen aber drawRect nicht aufgerufen.
Jedes Feedback ist willkommen.
Alles anzeigen
ich möchte in einem Statistik View einen Balken zeichnen, der je nach Erfüllungsgrad zu x Prozent gefüllt ist.
Das Füllen sollte animiert werden.
Was ich bisher getan habe, funktioniert leider nicht. Ist mein Ansatz falsch?
In ViewDidLoad habe ich den subView angelegt und drawRect zeichnet auch das Rechteck.
In ViewDidAppear wollte ich eine Animation starten aber so scheint das nicht zu funktionieren.
Der Wert wird zugewiesen aber drawRect nicht aufgerufen.
Jedes Feedback ist willkommen.
Quellcode
- @interface CycleResultsView : UIView {
- NSInteger total;
- NSInteger correct;
- }
- @property (nonatomic, assign) NSInteger total;
- @property (nonatomic, assign) NSInteger correct;
- - (id)initWithFrame:(CGRect)frame total:(NSInteger)numberOfQuestions;
- - (void)setCorrect:(NSInteger)value;
- @end
- @implementation CycleResultsView
- @synthesize total, correct;
- - (void)setCorrect:(NSInteger)value {
- correct = value;
- [self setNeedsDisplay];
- }
- - (id)initWithFrame:(CGRect)frame total:(NSInteger)numberOfQuestions {
- self = [super initWithFrame:frame];
- if (self) {
- total = numberOfQuestions;
- }
- return self;
- }
- static CGColorSpaceRef getTheRGBColorSpace(void)
- {
- static CGColorSpaceRef deviceRGB = NULL;
- // Set once, the first time this function is called.
- if(deviceRGB == NULL)
- deviceRGB = CGColorSpaceCreateDeviceRGB();
- return deviceRGB;
- }
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGColorSpaceRef theColorSpace = getTheRGBColorSpace();
- CGContextSetFillColorSpace(context, theColorSpace);
- CGContextSetStrokeColorSpace(context, theColorSpace);
- float greenWeb[] = { 0./255, 128./255, 0./255, 0.85 };
- CGContextSetFillColor(context, greenWeb);
- CGContextSetRGBStrokeColor(context, 0.000, 0.000, 0.000, 0.85);
- CGContextStrokeRect(context, self.bounds);
- if (correct) {
- CGRect rect = self.bounds;
- rect.size.width = self.bounds.size.width/total*correct;
- CGContextFillRect(context, rect);
- }
- }
- @end
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.myCycleResultsView = [[CycleResultsView alloc] initWithFrame:rect total:totalNumber];
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
- [UIView animateWithDuration:2.0
- delay:0
- options:UIViewAnimationOptionAllowAnimatedContent
- animations:^{self.myCycleResultsView.correct = answers;}
- completion:NULL];
- }