Fondu en fondu animation

Voici un code avec lequel je me bats pendant un moment.

Si vous démarrez l'animation de fondu, le text de l'label apparaît progressivement. Si je lance l'animation de fondu, le text de l'label disparaît.

Lorsque je lance la méthode startFade , seul le fondu sortant est affiché. Comment puis-je attendre que la méthode fadeIn se termine visuellement avant de démarrer la méthode fadeOut .

 -(IBAction)startFade:(id)sender{ [self fadeIn]; [self fadeOut]; } -(IBAction)fadeIn:(id)sender{ [self fadeIn]; } -(IBAction)fadeOut:(id)sender{ [self fadeOut]; } -(void) fadeIn{ [_label setAlpha:0]; [UILabel beginAnimations:NULL context:nil]; [UILabel setAnimationDuration:2.0]; [_label setAlpha:1]; [UILabel commitAnimations]; } -(void) fadeOut{ [UILabel beginAnimations:NULL context:nil]; [UILabel setAnimationDuration:2.0]; [_label setAlpha:0]; [UILabel commitAnimations]; } 

Lorsque vous appelez les methods fadeIn et fadeOut après les autres, le code est exécuté instantanément, de sorte que vous ne voyez que l'animation de la dernière méthode appelée. L'animation basée sur un bloc UIView fournit un gestionnaire d'achèvement, qui semble être exactement ce que vous cherchez. Votre code pourrait ressembler à ceci:

 -(IBAction)startFade:(id)sender { [_label setAlpha:0.0f]; //fade in [UIView animateWithDuration:2.0f animations:^{ [_label setAlpha:1.0f]; } completion:^(BOOL finished) { //fade out [UIView animateWithDuration:2.0f animations:^{ [_label setAlpha:0.0f]; } completion:nil]; }]; } 

Rapide:

 @IBAction func startFade(_ sender: AnyObject) { label.alpha = 0.0 // fade in UIView.animate(withDuration: 2.0, animations: { label.alpha = 1.0 }) { (finished) in // fade out UIView.animate(withDuration: 2.0, animations: { label.alpha = 0.0 }) } } 

cela fait le travail pour vous (le _label est votre label);

 - (IBAction)startFade:(id)sender { [_label setAlpha:0.f]; [UIView animateWithDuration:2.f delay:0.f options:UIViewAnimationOptionCurveEaseIn animations:^{ [_label setAlpha:1.f]; } completion:^(BOOL finished) { [UIView animateWithDuration:2.f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{ [_label setAlpha:0.f]; } completion:nil]; }]; } 

Essaye ça..

// Disparaître

  -(void)fadeOut:(UIView*)viewToDissolve withDuration:(NSTimeInterval)duration andWait:(NSTimeInterval)wait { [UIView beginAnimations: @"Fade Out" context:nil]; // wait for time before begin [UIView setAnimationDelay:wait]; // druation of animation [UIView setAnimationDuration:duration]; viewToDissolve.alpha = 0.0; [UIView commitAnimations]; } 

// Fade In

 -(void) fadeIn:(UIView*)viewToFadeIn withDuration:(NSTimeInterval)duration andWait:(NSTimeInterval)wait { [UIView beginAnimations: @"Fade In" context:nil]; // wait for time before begin [UIView setAnimationDelay:wait]; // druation of animation [UIView setAnimationDuration:duration]; viewToFadeIn.alpha = 1; [UIView commitAnimations]; } 

// Fondu de fondu sortant

 -(void) fadeInFromFadeOut: (UIView*)viewToFadeIn withDuration:(NSTimeInterval)duration { viewToFadeIn.hidden=NO; [self fadeOut:viewToFadeIn withDuration:1 andWait:0]; [self fadeIn:viewToFadeIn withDuration:duration andWait:0]; } 

// Opération de button

 -(void) buttonClicked :(id)sender { NSLog(@"Button clicked"); // Each button is given a tag int tag = ((UIButton*)sender).tag; if (tag ==1) { sprite.alpha =1; [self fadeOut : sprite withDuration: 10 andWait : 1 ]; } else if (tag ==2) { sprite.alpha =0; [self fadeIn : sprite withDuration: 3 andWait : 1 ]; } else { [self fadeInFromFadeOut:sprite withDuration:10]; } } 

Voir ce lien pour download l'exemple ..

Référez-vous à ce lien .

Heureux de partager avec vous .. 🙂

vous pouvez faire quelque chose comme ça (vérifiez les valeurs de parameters possibles et les methods similaires ici: https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html

 [UIView animateWithDuration:duration delay:delay options:option animations:^{ //fade in here (changing alpha of UILabel component) } completion:^(BOOL finished){ if(finished){ //start a fade out here when previous animation is finished (changing alpha of UILabel component) }]; } 

Basé sur la réponse de @ holex, mais simplifié un peu (comme commenté):

 - (IBAction)startFade:(id)sender { [_label setAlpha:0.f]; [UIView animateWithDuration:2.f delay:0.f options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAutoreverse animations:^{ [_label setAlpha:1.f]; } completion:nil]; } 

Le plus simple serait d'utiliser:

 [UIView animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion] 

et ajoutez l'appel fadeOut au bloc d' completion . La documentation pourrait aider à répondre à vos questions.

Si vous ne pouvez pas utiliser la version de bloc pour une raison quelconque, vous devrez définir un délégué ( [UIView setAnimationDelegate:(id)delegate] ) et un sélecteur avec ( [UIView setAnimationDidStopSelector:] ) auquel le délégué répondra .

Encore une fois, voir la documentation pour plus de détails.

Ma tâche était de faire disparaître un label. Et puis fondu avec le text modifié. La solution était:

  -(void)performAnimationOnHistoryButtonLabelUpdatingTextTo:(NSSsortingng *)text { [UIView animateWithDuration:0.4f animations:^{ [self.historyButtonLabel setAlpha:0.0f]; } completion:^(BOOL finished) { self.historyButtonLabel.text = text; [UIView animateWithDuration:0.4f animations:^{ [self.historyButtonLabel setAlpha:1.0f]; } completion:nil]; }]; } 
 labelAnimate = (UILabel*) [self.view viewWithTag:101]; btnTapMe = (UIButton*) [self.view viewWithTag:100]; [btnTapMe addTarget:self action:@selector(startAnimating:) forControlEvents:UIControlEventTouchUpInside]; ////////////// -(void) startAnimating:(UIButton*)button { [labelAnimate setAlpha:0.0]; [NSTimer scheduledTimerWithTimeInterval:1.8 target:self selector:@selector(continuousEaseInOut) userInfo:button repeats:YES]; } -(void) continuousFadeInOut { [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ [labelAnimate setAlpha:1.0]; } completion:^(BOOL finished) { [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ [labelAnimate setAlpha:0.0]; } completion:nil]; }]; } 

Réponse générique: Vous pouvez utiliser cette méthode pour appliquer une animation à n'importe quel object UIView. Créez d'abord une extension de la class UIView. Créez un file swift séparé et écrivez le code comme ceci

 import Foundation import UIKit extension UIView { func fadeIn(){ UIView.animateWithDuration(1.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: { self.alpha = 1.0 }, completion: nil) } func fadeOut(){ UIView.animateWithDuration(1.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { self.alpha = 0.0 }, completion: nil) } } 

Ici, self se réfère à n'importe quel UIView auquel vous vous référez. Vous pouvez utiliser des buttons, des labels, etc. pour appeler ces 2 methods.

Ensuite, dans toute autre class Swift, vous pouvez appeler fadeIn () et fadeOut () comme ceci:

 self.yourUIObject.fadeIn() self.yourUIObject.fadeOut() 

Cela donne l'effet désiré d'animation à n'importe quel UIObject.

Je vous suggère fortement d'utiliser une implémentation générique afin de pouvoir réutiliser le code chaque fois que vous aurez à nouveau besoin de l'effet de fondu.

Vous devriez créer une extension UIView :

UIView+Animations.h

 #import <Foundation/Foundation.h> @interface UIView (Animations) - (void)fadeInWithCompletion:(void (^ __nullable)(BOOL finished))completion; - (void)fadeOutWithCompletion:(void (^ __nullable)(BOOL finished))completion;; @end 

UIView+Animations.m

 #import "UIView+Animations.h" @implementation UIView (Animations) - (void)fadeInWithCompletion:(void (^ __nullable)(BOOL finished))completion { [UIView animateWithDuration:2 animations:^{ [self setAlpha:1]; } completion:completion]; } - (void)fadeOutWithCompletion:(void (^ __nullable)(BOOL finished))completion { [UIView animateWithDuration:2 animations:^{ [self setAlpha:0]; } completion:completion]; } @end 

Il suffit donc d'importer le nouveau file dans votre class ou dans votre Prefix.pch et de l'utiliser comme ceci:

 [_label fadeOutWithCompletion:^(BOOL finished) { [_label fadeInWithCompletion:nil]; }]; 

Notez que vous pouvez également utiliser nil comme paramètre d'achèvement lorsque vous n'avez rien d'autre à faire après l'achèvement.

Je vous recommand également de ne pas paramétrer la durée pour conserver un motif dans toute votre application.

Cette implémentation peut être utilisée dans le futur pour UIButton , UILabel , UITextField … Eh bien, n'importe quelle class dérivée de UIView .