ios - how to reset/restart an animation and have it appear continuous? -


so, new ios programming, , have inherited project former coworker. building app contains gauge ui. when data comes in, want smoothly rotate our "layer" (which needle image) current angle new target angle. here have, worked slow data:

-(void) moveneedletoangle:(float) target {    static float old_value = 0.0;    cabasicanimation *rotatecurrentpressuretick = [cabasicanimation animationwithkeypath:@"transform.rotation");    [rotatecurrentpressuretick setdelegate:self];    rotatecurrentpressuretick.fromvalue = [nssnumber numberwithfloat:old_value/57.2958];    rotatecurrentpressuretick.removedoncompletion=no;    rotatecurrentpressuretick.fillmode=kcafillmodeforwards;    rotatecurrentpressuretick.tovalue=[nssnumber numberwithfloat:target/57.2958];    rotatecurrentpressuretick.duration=3; // constant 3 second sweep     [imageview_needle.layer addanimation:rotatecurrentpressuretick forkey:@"rotatetick"];    old_value = target; } 

the problem have new data scheme in new data can come in (and above method called) faster, before animation complete. what's happening think animation restarted old target new target, makes jumpy.

so wondering how modify above function add continuous/restartable behavior, follows:

  1. check if current animation in progress ,
  2. if so, figure out current animation angle is, ,
  3. cancel current , start new animation current rotation new target rotation

is possible build behavior above function?

thanks. sorry if question seems uninformed, have studied , understand above objects/methods, not expert.

yes can using existing method, if add bit of magic:

    - (void)removeanimationsfromview:(uiview*)view {         calayer *layer = view.layer.presentationlayer;         cgaffinetransform transform = layer.affinetransform;         [layer removeallanimations];         view.transform = transform;     } 

the presentation layer encapsulates actual state of animation. view doesn't carry animation state properties, when set animation end state, view acquires state trigger animation. presentation layer 'see' during animation.

this method captures state of presentation layer @ exact moment cancel animation, , applies state view.

now can use method in animation method, this:

-(void) moveneedletoangle:(float) target{     [self removeanimationsfromview:imageview_needle];     id rotation = [imageview_needle valueforkeypath:@"layer.transform.rotation.z"];     cgfloat old_value = [rotation floatvalue]*57.2958;     // static float old_value = 0.0;     cabasicanimation *rotatecurrentpressuretick = [cabasicanimation animationwithkeypath:@"transform.rotation"];     [rotatecurrentpressuretick setdelegate:self];     rotatecurrentpressuretick.fromvalue = [nsnumber numberwithfloat:old_value/57.2958];     rotatecurrentpressuretick.removedoncompletion=no;     rotatecurrentpressuretick.fillmode=kcafillmodeforwards;     rotatecurrentpressuretick.tovalue=[nsnumber numberwithfloat:target/57.2958];     rotatecurrentpressuretick.duration=3; // constant 3 second sweep      [imageview_needle.layer addanimation:rotatecurrentpressuretick forkey:@"rotatetick"];      old_value = target;  } 

(i have made minimal changes method: there few coding style changes make, not relevant problem)

by way, suggest feed method in radians, not degrees, mean can remove 57.2958 constants.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -