核心动画:帧率

时间:2012-05-11 09:00:15

标签: objective-c ios xcode macos core-animation

我目前正在开发一个项目,我们正在实施一些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)会在线程等上放置更多优先级,因此可能会提高帧速率吗?有没有更好的方法来完全动画这些?

1 个答案:

答案 0 :(得分:6)

The documentation for NSAnimation

  

帧率为0.0意味着尽可能快......   帧速率无法保证

应尽可能快地合理地与60 fps相同。


使用核心动画代替NSAnimation

NSAnimation实际上并不是Core Animation的一部分(它是AppKit的一部分)。我建议尝试使用Core Animation作为动画。

  1. 将QuartzCore.framework添加到项目中
  2. 导入您的文件
  3. 在您正在设置动画的视图上将- (void)setWantsLayer:(BOOL)flag设置为YES
  4. 切换到Core Animation以获取类似
  5. 的动画

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

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