2012-05-11 3 views
0

Je travaille actuellement sur un projet dans lequel nous implémentons des animations de base pour redimensionner/déplacer des éléments. Nous avons remarqué sur de nombreux Mac que la fréquence d'images diminue considérablement au cours de ces animations, bien qu'elles soient plutôt simples. Voici un exemple:Core Animation: Frame Rate

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

Est indiquant explicitement le taux de trame (par exemple 60,0, au lieu de le laisser à 0.0) mettre une plus grande priorité sur les filets, etc., à cet effet augmenter éventuellement la fréquence d'images? Y a-t-il une meilleure façon de les animer?

Répondre

6

The documentation for NSAnimation dit

Une cadence de 0.0 signifie d'aller aussi vite que possible ... Le frame rate est pas garanti

Aussi vite que possible devrait, raisonnablement, être même que 60 fps.


Utilisation de Core Animation au lieu de NSAnimation

NSAnimation est pas vraiment une partie de Core Animation (son une tape de AppKit). Je recommanderais d'essayer Core Animation pour l'animation à la place.

  1. Ajout QuartzCore.framework à votre projet
  2. Importation dans votre fichier
  3. Réglage - (void)setWantsLayer:(BOOL)flag YES sur les vues que vous animez
  4. Passer au Core Animation pour quelque chose d'animation comme

de la durée de votre animation ci-dessus il ressemble à des "animations implicites" (juste en changeant la propriété de la couche) peut-être le mieux pour vous. Si toutefois vous voulez plus de contrôle, vous pouvez utiliser des animations explicites, quelque chose comme ceci:

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

Puis, en pour vous mettre en œuvre callbacks

- (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

Impressionnant !! Un bon conseil David merci - après l'implémentation, la performance n'est même pas comparable à NSAnimation. Beaucoup, beaucoup plus vite – Zakman411