2010-05-08 9 views

Respuesta

5

Puede implementar su propia vista personalizada que utilice una Animación Core CALayer para almacenar la imagen. Cuando configura la propiedad contents de la capa, la imagen se animará de forma automática y sin problemas desde la imagen anterior a la nueva.

+1

Eso suena bien. ¿Cómo hago eso, sin embargo? –

7

Por lo que sé, NSImageView no admite la animación de cambios de imagen. Sin embargo, puede colocar un segundo NSImageView encima del primero y animarlo ocultando el anterior y mostrando el nuevo. Por ejemplo:

NSImageView *newImageView = [[NSImageView alloc] initWithFrame: [imageView frame]]; 
[newImageView setImageFrameStyle: [imageView imageFrameStyle]]; 
// anything else you need to copy properties from the old image view 
// ...or unarchive it from a nib 

[newImageView setImage: [NSImage imageNamed: @"NSAdvanced"]]; 
[[imageView superview] addSubview: newImageView 
         positioned: NSWindowAbove relativeTo: imageView]; 
[newImageView release]; 

NSDictionary *fadeIn = [NSDictionary dictionaryWithObjectsAndKeys: 
      newImageView, NSViewAnimationTargetKey, 
      NSViewAnimationFadeInEffect, NSViewAnimationEffectKey, 
      nil]; 
NSDictionary *fadeOut = [NSDictionary dictionaryWithObjectsAndKeys: 
      imageView, NSViewAnimationTargetKey, 
      NSViewAnimationFadeOutEffect, NSViewAnimationEffectKey, 
      nil]; 
NSViewAnimation *animation = [[NSViewAnimation alloc] initWithViewAnimations: 
       [NSArray arrayWithObjects: fadeOut, fadeIn, nil]]; 
[animation setAnimationBlockingMode: NSAnimationBlocking]; 
[animation setDuration: 2.0]; 
[animation setAnimationCurve: NSAnimationEaseInOut]; 
[animation startAnimation]; 
[imageView removeFromSuperview]; 
imageView = newImageView; 
[animation release]; 

Si su visión es grande y se puede requerir 10.5 +, entonces se podría hacer lo mismo con Core Animation, que será acelerada por hardware y utilizar mucho menos CPU.

Después de crear newImageView, hacer algo como:

[newImageView setAlphaValue: 0]; 
[newImageView setWantsLayer: YES]; 
// ... 
[self performSelector: @selector(animateNewImageView:) withObject: newImageView afterDelay: 0]; 

- (void)animateNewImageView:(NSImageView *)newImageView; 
{ 
    [NSAnimationContext beginGrouping]; 
    [[NSAnimationContext currentContext] setDuration: 2]; 
    [[newImageView animator] setAlphaValue: 1]; 
    [[imageView animator] setAlphaValue: 0]; 
    [NSAnimationContext endGrouping]; 
} 

Tendrá que modificar el anterior para ser abortable, pero no voy a escribir todo el código para usted :-)

+0

Quiero forzar a que la animación se detenga en el medio; ¿es eso posible? –

+0

Claro, si usa NSAnimationNonblocking o NSAnimationNonblockingThreaded y mantiene una referencia al objeto NSViewAnimation en alguna parte, puede invocar [animation stopAnimation] y luego devolver las vistas de la forma que desee. –

+0

Está bien, intenté hacer eso, pero estaba realmente agitado. ¿Sabes cómo hacer el método de Rob Keniger? Eso parece ser el más simple. –

Cuestiones relacionadas