ios - Core Animation: Setting animated property correctly -
i trying build bar graph view using core animation , layers. make cooler tried let each beam pop bit, 1 after another. convenience flipped coordinate system of view vertically.
here's code:
- (id)initwithframe:(cgrect)frame {     self = [super initwithframe:frame];     if (self) {         self.transform = cgaffinetransformmakescale(1, -1);         self.values = @[@12.5f, @4.25f, @23.0f, @3.0f, @17.9f, @7.0f, @15.1f];     }     return self; }  - (void)didmovetosuperview {     self.backgroundcolor = [uicolor whitecolor];     self.beamcontainer = [calayer layer];     cgrect frame = cgrectinset(self.bounds, 20, 20);     self.beamcontainer.frame = frame;     self.beamcontainer.backgroundcolor = [uicolor colorwithwhite:0.98 alpha:1].cgcolor;     float maxvalue = [[self.values valueforkeypath:@"@max.floatvalue"] floatvalue];     (int = 0; < self.values.count; i++) {         calayer *beam = [calayer layer];         cgfloat beamheight = ([self.values[i] floatvalue] * frame.size.height) / maxvalue;          beam.backgroundcolor = [uicolor colorwithred:0.5 green:0.6 blue:1 alpha:1].cgcolor;         beam.anchorpoint = cgpointmake(0, 0);         beam.position = cgpointmake(frame.size.width * ((float)i/(float)self.values.count), 0);         cgrect endbounds = cgrectmake(0, 0, frame.size.width /(float)self.values.count, beamheight);         cabasicanimation *animation = [cabasicanimation animationwithkeypath:@"bounds"];         animation.fromvalue = [nsvalue valuewithcgrect:cgrectmake(0, 0, frame.size.width /(float)self.values.count, 5)];         animation.tovalue = [nsvalue valuewithcgrect:endbounds];         animation.duration = .5;         animation.begintime = cacurrentmediatime() + ((float)i * 0.1);         [beam addanimation:animation forkey:@"beamanimation"];          [self.beamcontainer addsublayer:beam];         beam.bounds = endbounds;     }     [self.layer addsublayer:self.beamcontainer]; } this works charm, problem if don't write beam.bounds = endbounds; beam snap it's old bounds after animation finished. when do, it's going use endbounds before animation has started , during delay each animation.
how can animate beam going bounds b , sticking b @ end?
you try following:
// save original value cgfloat originaly = layer.position.y;  // change model value (this not animated yet) layer.position = cgpointmake(layer.position.x, 300.0);  cabasicanimation *animation = [cabasicanimation animationwithkeypath:@"position.y"];  // specify fromvalue animation because // current model value correct tovalue animation.fromvalue = @(originaly); animation.duration = 1.0;  // use name of animated property key // override implicit animation [layer addanimation:animation forkey:@"position"]; although not exact match bar graph animations trying, can pattern above example. you can read more here. helped me similar issue had other day.
Comments
Post a Comment