淡入淡出animation

这里有一段代码我在一段时间里苦苦挣扎。

如果在animation中开始淡入淡出,则标签文本将淡入。如果我开始淡出animation,标签文本将淡出。

当我启动startFade方法时,只显示淡出。 如何等待fadeIn方法在启动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]; } 

当你像你一样调用fadeInfadeOut方法时,代码会立即运行,所以你只能看到最后一个方法的animation。 基于UIView块基于animation提供了一个完成处理程序,这似乎正是你在找什么。 所以你的代码看起来像这样:

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

迅速:

 @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 }) } } 

为你做的工作(标签是你的标签);

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

尝试这个..

// 消退

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

// 淡入

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

//淡入淡出

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

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

查看此链接下载示例

参考这个链接 。

很高兴与大家分享.. 🙂

你可以做这样的事情(检查可能的参数值和类似的方法在这里: 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) }]; } 

基于@ holex的答案,但简化了一些(如评论):

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

最简单的方法是使用:

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

并将fadeOut调用添加到completion块。 该文档可能有助于解答您的任何问题。

如果由于某种原因你不能使用块版本,那么你必须设置一个委托( [UIView setAnimationDelegate:(id)delegate] )和一个select器( [UIView setAnimationDidStopSelector:] ),委托将响应。

再次请参阅文档以获取更多详细信息。

我的任务是让一个标签淡出。 然后淡入,改变文字。 解决scheme是:

  -(void)performAnimationOnHistoryButtonLabelUpdatingTextTo:(NSString *)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]; }]; } 

通用答案:您可以使用此方法将animation应用于任何UIView对象。 首先创build一个UIView类的扩展。 创build一个单独的swift文件,并像这样写代码

 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) } } 

这里的self指的是你引用的任何UIView。 你可以使用button,标签等来调用这两种方法。

然后在任何其他的Swift类中,你可以像这样调用fadeIn()和fadeOut():

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

这给了任何UIObject所需的animation效果。

我强烈build议您使用通用的实现,以便您可以重新使用代码,每当你需要淡入淡出效果。

你应该创build一个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 

所以,你只需要将新文件导入到你的类或Prefix.pch ,并像这样使用它:

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

请注意,如果完成后没有其他任何事情可以使用nil作为完成参数。

我也build议你不要参数化持续时间来保持整个应用程序的模式。

这个实现可以在将来用于UIButtonUILabelUITextField …那么,从UIView派生的任何类。