2012-05-11 99 views
0

我目前正在研究一个项目,我们正在实施一些核心动画来调整大小/移动元素。我们已经注意到,在这些动画中,许多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离开它)把更多的优先考虑线程等等,为此可能提高帧速率?有没有更好的方法来完成这些动画?

回答

6

The documentation for NSAnimation

0.0帧速率意味着尽可能快... 帧速率不保证

为尽快应该去,合理,是与60 fps相同。


使用的Core Animation,而不是NSAnimation

NSAnimation是不是真正的Core Animation(它了AppKit的PAT)的一部分。我会建议尝试核心动画的动画来代替。

  1. 添加QuartzCore.framework到项目
  2. 在你的文件中导入
  3. 在您设置动画
  4. 切换到核心动画的看法为动画设置东西向- (void)setWantsLayer:(BOOL)flag就像是

从上面的动画持续时间开始,它看起来像“隐式动画”(只是更改图层的属性)可能最适合您。但如果你想要更多的控制,你可以使用显式的动画,这样的事情:

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. 
} 
+0

真棒!伟大的技巧大卫感谢 - 实施这个后,性能甚至不能与NSAnimation相提并论。更快,更快 – Zakman411