Animation & drawRect

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Animation & drawRect

    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.


    Quellcode

    1. @interface CycleResultsView : UIView {
    2. NSInteger total;
    3. NSInteger correct;
    4. }
    5. @property (nonatomic, assign) NSInteger total;
    6. @property (nonatomic, assign) NSInteger correct;
    7. - (id)initWithFrame:(CGRect)frame total:(NSInteger)numberOfQuestions;
    8. - (void)setCorrect:(NSInteger)value;
    9. @end
    10. @implementation CycleResultsView
    11. @synthesize total, correct;
    12. - (void)setCorrect:(NSInteger)value {
    13. correct = value;
    14. [self setNeedsDisplay];
    15. }
    16. - (id)initWithFrame:(CGRect)frame total:(NSInteger)numberOfQuestions {
    17. self = [super initWithFrame:frame];
    18. if (self) {
    19. total = numberOfQuestions;
    20. }
    21. return self;
    22. }
    23. static CGColorSpaceRef getTheRGBColorSpace(void)
    24. {
    25. static CGColorSpaceRef deviceRGB = NULL;
    26. // Set once, the first time this function is called.
    27. if(deviceRGB == NULL)
    28. deviceRGB = CGColorSpaceCreateDeviceRGB();
    29. return deviceRGB;
    30. }
    31. // Only override drawRect: if you perform custom drawing.
    32. // An empty implementation adversely affects performance during animation.
    33. - (void)drawRect:(CGRect)rect {
    34. CGContextRef context = UIGraphicsGetCurrentContext();
    35. CGColorSpaceRef theColorSpace = getTheRGBColorSpace();
    36. CGContextSetFillColorSpace(context, theColorSpace);
    37. CGContextSetStrokeColorSpace(context, theColorSpace);
    38. float greenWeb[] = { 0./255, 128./255, 0./255, 0.85 };
    39. CGContextSetFillColor(context, greenWeb);
    40. CGContextSetRGBStrokeColor(context, 0.000, 0.000, 0.000, 0.85);
    41. CGContextStrokeRect(context, self.bounds);
    42. if (correct) {
    43. CGRect rect = self.bounds;
    44. rect.size.width = self.bounds.size.width/total*correct;
    45. CGContextFillRect(context, rect);
    46. }
    47. }
    48. @end
    49. - (void)viewDidLoad {
    50. [super viewDidLoad];
    51. self.myCycleResultsView = [[CycleResultsView alloc] initWithFrame:rect total:totalNumber];
    52. }
    53. - (void)viewDidAppear:(BOOL)animated {
    54. [super viewDidAppear:animated];
    55. [UIView animateWithDuration:2.0
    56. delay:0
    57. options:UIViewAnimationOptionAllowAnimatedContent
    58. animations:^{self.myCycleResultsView.correct = answers;}
    59. completion:NULL];
    60. }
    Alles anzeigen


  • Wenn Du für die Animation CoreAnimation verwenden möchtest, was sehr zu empfehlen ist, dann solltest Du die Darstellung über Views machen. Am einfachsten verwendest Du UIViews mit der entsprechenden Hintergrundfarbe oder UIImageViews. Die Füllhöhe der Balken legst Du über die Größe des Frames fest. Diesen Frame kannst Du dann über implizite oder explizite Animationen verändern.

    Das ist viel einfacher, als es sich anhört. ;)
    „Meine Komplikation hatte eine Komplikation.“