这是我挣扎了一段时间的一些代码。
如果您启动淡入动画,则标签文字会淡入。 如果我开始淡出动画,标签文本会淡出。
当我启动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];
}
答案 0 :(得分:110)
当您像往常一样背靠背调用fadeIn
和fadeOut
方法时,代码会立即运行,因此您只能看到上一个调用方法的动画。基于UIView块的动画提供了一个完成处理程序,它似乎正是您正在寻找的。所以你的代码看起来像这样:
-(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
})
}
}
答案 1 :(得分:32)
为你做的工作(_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];
}];
}
答案 2 :(得分:6)
试试这个..
//淡出
-(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];
}
//按钮操作
-(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];
}
}
查看此link以下载示例..
请参阅此link。
很高兴与您分享..: - )
答案 3 :(得分:3)
通用答案:您可以使用此方法将动画应用于任何UIView对象。 首先创建UIView类的扩展。创建一个单独的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)
}
}
这里的自我指的是你所指的任何UIView。您可以使用按钮,标签等来调用这两种方法。
然后在任何其他swift类中,你可以像这样调用fadeIn()和fadeOut():
self.yourUIObject.fadeIn()
self.yourUIObject.fadeOut()
这为任何UIObject提供了所需的动画效果。
答案 4 :(得分:2)
您可以执行以下操作(请在此处检查可能的参数值和类似方法: 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)
}];
}
答案 5 :(得分:2)
基于@ 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];
}
答案 6 :(得分:1)
最简单的方法是使用:
[UIView animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion]
并将fadeOut
调用添加到completion
块。 The documentation可能有助于回答您的任何问题。
如果由于某种原因无法使用块版本,那么您必须设置委托将响应的委托([UIView setAnimationDelegate:(id)delegate]
)和选择器([UIView setAnimationDidStopSelector:]
)。
再次,请参阅the documentation了解详情。
答案 7 :(得分:1)
我的任务是让标签淡出。然后用改变后的文字淡入淡出。解决方案是:
-(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];
}];
}
答案 8 :(得分:1)
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];
}];
}
答案 9 :(得分:1)
我强烈建议您使用通用实现,以便在需要淡入淡出效果时重复使用代码。
您应该创建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
作为完成参数。
我还建议您不要参数化持续时间以保持整个应用程序的模式。
此实现可以在将来用于UIButton
,UILabel
,UITextField
......好吧,任何来自UIView
的类。
答案 10 :(得分:1)
快捷键4 如果单击按钮时仅需要一个脉冲,请使用:
@IBAction func didPressButton(_ sender: UIButton) {
self.someView.alpha = 0
UIView.animate(withDuration: 0.4,
delay: 0,
options: [.curveEaseInOut, .autoreverse],
animations: {
self.someView.alpha = 1
},
completion: { _ in
self.someView.alpha = 0
})
}
答案 11 :(得分:0)
可以使用UIView.animate(withDuration: animations:)
UIView.animate(withDuration: animationDuration, animations: {
myView.alpha = 0.75
myView.alpha = 1.0
})
答案 12 :(得分:0)
Swift 5版本的iPhoneDeveloper的答案:
extension UIView {
func fadeIn() {
UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveEaseIn, animations: {
self.alpha = 1.0
}, completion: nil)
}
func fadeOut() {
UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveEaseOut, animations: {
self.alpha = 0.0
}, completion: nil)
}
}