我正在编写代码,以便在窗口中移动NSTextView.我希望用户可以设置持续时间.
这个版本工作正常:
// 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];
这个将时间保留为默认值:
// 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)];
任何人都可以告诉我为什么第一个工作,第二个不工作?他们似乎与我相当.
谢谢!
最佳答案 对于您想要使用的CABasicAnimation
[CATransaction setAnimationDuration:yourDuration];