Mostrar y desvanecer UIImageView después de 2 Segundos


Estoy trabajando en un sistema de notificaciones en mi juego de iPhone y quiero que una imagen aparezca en la pantalla y se desvanezca automáticamente después de 2 segundos.

  1. El usuario hace clic en un botón que llama al método "popupImage"
  2. Una imagen aparece en la pantalla en un lugar designado, no necesita desvanecerse en
  3. La imagen se desvanece por sí misma después de estar en la pantalla durante 2 segundos.

¿Hay alguna manera de hacer esto? Gracias por adelantado.

Author: felix_xiao, 2012-09-18

4 answers

Use UIView métodos dedicados para eso.

Así que imagine que ya tiene su UIImageView listo, ya creado y agregado a la vista principal, pero simplemente oculto. Su método simplemente necesita hacerlo visible, e iniciar una animación 2 segundos después para desvanecerla, animando su propiedad "alpha" de 1.0 a 0.0 (durante una animación de 0.5 s):

-(IBAction)popupImage
{
    imageView.hidden = NO;
    imageView.alpha = 1.0f;
    // Then fades it away after 2 seconds (the cross-fade animation will take 0.5s)
    [UIView animateWithDuration:0.5 delay:2.0 options:0 animations:^{
         // Animate the alpha value of your imageView from 1.0 to 0.0 here
         imageView.alpha = 0.0f;
     } completion:^(BOOL finished) {
         // Once the animation is completed and the alpha has gone to 0.0, hide the view for good
         imageView.hidden = YES;
     }];
}

¡Tan simple como eso!

 53
Author: AliSoftware,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-17 22:06:50

En Swift y XCode 6

self.overlay.hidden = false
UIView.animateWithDuration(2, delay:5, options:UIViewAnimationOptions.TransitionFlipFromTop, animations: {
    self.overlay.alpha = 0
    }, completion: { finished in
    self.overlay.hidden = true
})

Donde la superposición es la salida para mi imagen.

 11
Author: Joseph Selvaraj,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-25 17:20:04

Swift 3 versión de @AliSoftware ' s respuesta

imageView.isHidden = false
imageView.alpha = 1.0

UIView.animate(withDuration: 0.5, delay: 2.0, options: [], animations: {

            self.imageView.alpha = 0.0

        }) { (finished: Bool) in

            self.imageView.isHidden = true
        }
 2
Author: iUser,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-11 18:32:48

Sí lo hay. Echa un vistazo a UIViewanimación basada en bloques aquí. Y google, por ejemplo.

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations

También puede iniciar un temporizador

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
 0
Author: ohr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-17 21:46:02