我目前正在开发一个项目,我们正在实施一些Core Animation来调整大小/移动元素。我们已经注意到在许多Mac上,帧速率在这些动画中显着下降,尽管它们相当简单。这是一个例子:
// Set some additional attributes for the animation.
[theAnim setDuration:0.25]; // Time
[theAnim setFrameRate:0.0];
[theAnim setAnimationCurve:NSAnimationEaseInOut];
// Run the animation.
[theAnim startAnimation];
[self performSelector:@selector(endAnimation) withObject:self afterDelay:0.25];
明确说明帧速率(比如60.0,而不是将其保留为0.0)会在线程等上放置更多优先级,因此可能会提高帧速率吗?有没有更好的方法来完全动画这些?
答案 0 :(得分:6)
The documentation for NSAnimation说
帧率为0.0意味着尽可能快...... 帧速率无法保证
应尽可能快地合理地与60 fps相同。
NSAnimation实际上并不是Core Animation的一部分(它是AppKit的一部分)。我建议尝试使用Core Animation作为动画。
- (void)setWantsLayer:(BOOL)flag
设置为YES 从上面的动画持续时间看起来像“隐式动画”(只是更改图层的属性)可能最适合你。但是,如果您想要更多控制,可以使用显式动画,如下所示:
CABasicAnimation * moveAnimation = [CABasicAnimation animationWithKeyPath:@"frame"];
[moveAnimation setDuration:0.25];
// There is no frame rate in Core Animation
[moveAnimation setTimingFunction:[CAMediaTimingFunction funtionWithName: kCAMediaTimingFunctionEaseInEaseOut]];
[moveAnimation setFromValue:[NSValue valueWithCGRect:yourOldFrame]]
[moveAnimation setToValue:[NSValue valueWithCGRect:yourNewFrame]];
// To do stuff when the animation finishes, become the delegate (there is no protocol)
[moveAnimation setDelegate:self];
// Core Animation only animates (not changes the value so it needs to be set as well)
[theViewYouAreAnimating setFrame:yourNewFrame];
// Add the animation to the layer that you
[[theViewYouAreAnimating layer] addAnimation:moveAnimation forKey:@"myMoveAnimation"];
然后在回调中实施
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)isFinished {
// Check the animation and perform whatever you want here
// if isFinished then the animation completed, otherwise it
// was cancelled.
}