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];

フレーム レートを明示的に指定すると (0.0 のままにするのではなく、60.0 など)、スレッドなどの優先順位が高くなり、フレーム レートが上がる可能性がありますか? これらを完全にアニメーション化するより良い方法はありますか?

4

1 に答える 1

6

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 に切り替えます

上記のアニメーションの長さから、「暗黙のアニメーション」(レイヤーのプロパティを変更するだけ)が最適なようです。ただし、より詳細な制御が必要な場合は、次のような明示的なアニメーションを使用できます。

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.
}
于 2012-05-11T20:05:23.530 に答える