I am writing code that will animate the move of an NSTextView across a window. I want the duration to be settable by the user.
This version works fine:
// code to setup TextView
[centerView addSubview:targetTextView];
[NSAnimationContext beginGrouping];
float finalDuration = expf((11-duration)/5) + 2;
[[NSAnimationContext currentContext] setDuration:(finalDuration)];
[[targetTextView animator]setFrame:NSMakeRect(-170,(centerViewRect.size.height * 1/2) - 40,160,80)];
[NSAnimationContext endGrouping];
This one leaves the timing at the default value:
// code to setup TextView
[centerView addSubview:targetTextView];
CABasicAnimation *posAnim = [CABasicAnimation animationWithKeyPath:@"frame"];
[posAnim setFromValue:[NSValue valueWithRect:NSMakeRect(centerViewRect.size.width - 170,(centerViewRect.size.height * 2/3) - 40,160,80)]];
[posAnim setToValue:[NSValue valueWithRect:NSMakeRect(10,(centerViewRect.size.height * 2/3) - 40,160,80)]];
float finalDuration = expf((11-duration)/5) + 2;
[posAnim setDuration:finalDuration];
[targetTextView setAnimations:[NSDictionary dictionaryWithObjectsAndKeys:posAnim,@"frame",nil]];
[[targetTextView animator]setFrame:NSMakeRect(-170,(centerViewRect.size.height * 1/2) - 40,160,80)];
Can anyone tell me why the first one works and the second one doesn’t? They seem to be equivalent to me.
Thanks!
For the CABasicAnimation you want to use
[CATransaction setAnimationDuration:yourDuration];