2012-09-17 17 views
24

Sto lavorando a un sistema di notifica nel mio gioco per iPhone e voglio che un'immagine compaia sullo schermo e svanisca automaticamente dopo 2 secondi. scattaShow e Fade UIImageView dopo 2 secondi

  1. utente un pulsante che richiama il metodo "popupImage"
  2. un'immagine appare sullo schermo in un posto designato, che non ha bisogno di dissolvenza in
  3. L'immagine scompare da sola dopo essere stato sullo schermo per 2 secondi.

Esiste un modo per farlo? Grazie in anticipo.

risposta

53

Usa UIView metodi dedicati per questo.

Immagina di avere già il tuo UIImageView pronto, già creato e aggiunto alla vista principale, ma semplicemente nascosto. Il tuo metodo semplicemente bisogno di renderlo visibile e avviare un'animazione 2 secondi più tardi a svanire fuori, animando la sua proprietà "alfa" 1,0-0,0 (durante un'animazione 0.5s):

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

Semplice come quello !

+0

Grazie per questo! –

+0

Grazie mi aiuta. – Shivaay

+0

http://stackoverflow.com/a/29080894/1442541 – evya

0

Sì, c'è. Dai un'occhiata all'animazione basata su blocco UIViewhere. E google per un esempio.

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

È possibile anche avviare una timer

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats 
10

In Swift e 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 
}) 

dove overlay è presa per la mia immagine.

2

Swift 3 versione di risposta @AliSoftware s'

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 
     } 
Problemi correlati