Quiero cambiar la imagen que se muestra en un NSImageView
, pero quiero animar ese cambio. He intentado varios métodos para hacer esto. Con suerte, uno de ustedes podría sugerir uno que en realidad podría funcionar. Estoy trabajando con Cocoa para Mac.¿Cómo puedo animar un interruptor de contenido en un NSImageView?
Respuesta
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.
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 :-)
Quiero forzar a que la animación se detenga en el medio; ¿es eso posible? –
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. –
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. –
- 1. Cómo animar un UIBezierPath
- 2. Cómo animar un favicon?
- 3. Obtener los límites de un NSImage dentro de un NSImageView
- 4. Cómo animar un personaje en 3D?
- 5. Agregar una sombra a un NSImageView
- 6. NSImageView + Color de fondo
- 7. GIF animado en NSImageView
- 8. ¿Cómo puedo animar un UIButton entre dos imágenes PNG?
- 9. Animar un MKOverlayView
- 10. ¿Cómo puedo animar MKAnnotationView drop?
- 11. Android: animar un rectángulo
- 12. Animar filas en un NSTableView
- 13. Cómo utilizar un UIButton como un interruptor de palanca
- 14. ¿Cómo crear un NSImageView con una esquina redondeada?
- 15. ¿Cómo puedo ver un archivo comprimido contenido
- 16. Cómo animar un diagrama de dispersión?
- 17. Cómo animar el dibujo de un CGPath?
- 18. Animar una fila en un NSTableView
- 19. Animar un encabezado y pie de página de UITableView
- 20. Animar el cambio de altura de un div cuando su contenido cambia
- 21. ¿Cómo puedo imprimir el contenido de un hash en Perl?
- 22. ¿cómo puedo animar los límites de un CAlayer para revelar progresivamente una imagen?
- 23. Cómo marcar un parámetro de interruptor como obligatorio en Powershell
- 24. jQuery: ¿cómo puedo animar una rotación div?
- 25. ¿Cómo puedo animar CATransform3Ds con CAKeyframeAnimation?
- 26. ¿Cómo puedo mostrar el contenido de un archivo en un estado específico de un git repo?
- 27. Zoom de acercamiento de imagen en NSImageView
- 28. ¿Cómo puedo obtener un contenido div en php
- 29. NSImageView esquinas redondeadas + carrera
- 30. ¿Es posible animar un UIImage?
Eso suena bien. ¿Cómo hago eso, sin embargo? –