ios - Core Animation speed -
i'd create animation similar swiping in menus of iphone. means if swipe enough, animation takes new page, if swipe little animation takes on starting page after finishing swipe gesture.
so far, have swipe recognizer listens position of finger on screen , follows movement of finger. animation looks @ end of gesture whether finger moved more 1/3 of screen width , decides whether proceed end or go beginning.
- (void)pangesturehandler:(uipangesturerecognizer*)recognizer { if (recognizer.state == uigesturerecognizerstateended) { //if our gesture enough let animation roll end if (fabsf(translation.x) > cgrectgetwidth(self.view.frame) * 0.33) { [self finishlayer:categorycontrollermain.view.layer animation:menuopenswipeanimatemoveleft withforward:no]; } //finger movement below .33% of screen width animate initial state else { [self finishlayer:categorycontrollermain.view.layer animation:menuopenswipeanimatemoveleft withforward:no]; } } else if (recognizer.state == uigesturerecognizerstatechanged) { //we put layer speed 0 can control animation timeoffset in relation finger pan movement categorycontrollermain.view.layer.speed = 0.0; [categorycontrollermain.view.layer addanimation:menuopenswipeanimatemoveleft forkey:@"menuopenmoveleft"]; categorycontrollermain.view.layer.timeoffset = fabs(translation.x / cgrectgetwidth(self.view.frame)); } }
and method says animation whether move towards end or beginning, according finger amount movement. if swiped bit, animation continues speed = -1;
, i.e. starts going backwards, when gets end, or in case beginning, layers disappear. here not case of presentationlayer
, because when animation ends, layers should @ exact point, because initial point. instead, layers gone, , when try swipe again, appear on correct locations.
- (void)finishlayer:(calayer*)layer animation:(cakeyframeanimation*)animation withforward:(bool)shouldgoforward { pan.enabled = no; if (shouldgoforward) { //animation continues cftimeinterval pausedtime = [layer converttime:cacurrentmediatime() fromlayer:nil]; layer.timeoffset = 0.0; layer.begintime = 0.0; cftimeinterval timesincepause = [layer converttime:cacurrentmediatime() fromlayer:nil] - pausedtime; layer.begintime = timesincepause; layer.speed = 1.0; } else { //we want take back. strange things happen. perhaps timeoffset not right layer.timeoffset = [layer converttime:cacurrentmediatime() fromlayer:nil]; layer.begintime = cacurrentmediatime(); layer.speed = -1; } }
a final consideration: if animations linear, create animation every animation beginning, calculate start , make appear 1 animation. animations have camediatimingfunction
s don't allow me calculate exact position in opposite animation.
edit
the question following: how perform core animation speed = -1
when animations rolls backwards, of layers/views stay visible on screen?
thanks in advance
ok, if ever needs solve similar issue, here deal.
you shouldn't use same animation object. correct answer set speed = -1.0
, not on animation you'd been using previously. instead, create new animation same values except of speed
should set -1. in case, have brand new animation not going have problems caused change of speed
.
the have careful set repeatduration
though. set timeoffset
property if leave duration same value original animation, end , start over. so, way cut length set value of repeatduration
exact value timeoffset
. stops @ last frame of original animation , works expected.
as mentioned before, using same animation wasn't option because when animation reached end (or might beginning), layer
disappear.
righty, here's code takes initial state following same properties of initial animation.
cgfloat elapsedtime = [layer converttime:cacurrentmediatime() fromlayer:nil]; layer.speed = 1; cakeyframeanimation* animationbackwards = [animation copy]; animationbackwards.speed = -1.0; animationbackwards.timeoffset = elapsedtime; animationbackwards.repeatduration = elapsedtime; [layer removeanimationforkey:animationname]; [layer addanimation:animationbackwards forkey:animationname];
Comments
Post a Comment