核心动画:帧率

Zak*_*411 0 macos xcode core-animation objective-c ios

我目前正在开发一个项目,我们正在实现一些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];
Run Code Online (Sandbox Code Playgroud)

明确说明帧速率(比如60.0,而不是将其保留为0.0)会在线程等上放置更多优先级,因此可能提高帧速率?有没有更好的方法来完全动画这些?

Dav*_*ist 6

NSAnimation的文档

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

合理地,应该尽可能快地与60 fps相同.


使用Core Animation代替NSAnimation

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

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

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

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"];
Run Code Online (Sandbox Code Playgroud)

然后进入你实现的回调

- (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.
}
Run Code Online (Sandbox Code Playgroud)