[英]How to Animate uilabel text between 3 values
我想要的是重复地在3个值之间动画我的UILabel文本。 我要设置的文字是“下载”,“下载..”,“下载...”,然后重复。 有了这个动画,我想让我的用户知道正在完成下载并且应用程序没有堆叠。 我一直在谷歌搜索一段时间,但没有找到解决方案。
任何人都可以给我一些参考吗? 提前致谢。
使用NStimer更改文本
在.h
文件中
@interface ViewController : UIViewController
{
NSTimer * timer;
UILabel * downloadingLbl;
}
并在.m
文件中
- (void)viewDidLoad
{
[super viewDidLoad];
downloadingLbl.text = @"Downloading.";
if (!timer) {
timer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(onTick:) userInfo:nil repeats:YES];
}
}
-(void)onTick:(NSTimer*)timer
{
NSLog(@"Tick...");
if ([downloadingLbl.text isEqualToString:@"Downloading."]) {
downloadingLbl.text = @"Downloading..";
} else if ([downloadingLbl.text isEqualToString:@"Downloading.."]) {
downloadingLbl.text = @"Downloading...";
} else if ([downloadingLbl.text isEqualToString:@"Downloading..."]) {
downloadingLbl.text = @"Downloading.";
}
}
当你的下载完成,使计时器失效。
[timer invalidate];
为此你必须像这样安排计时器:
int count = 1;
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(updateLabel)
userInfo:nil
repeats:YES];
现在,下面的方法将在每个(几秒)时间内被调用
- (void) updateLabel {
if(count == 1)
{
label.text = @"Downloading."
count = 2;
}
else if(count == 2)
{
label.text = @"Downloading.."
count = 3;
}
else if(count == 3)
{
label.text = @"Downloading..."
count = 1;
}
}
每当你想停止更新时,在你想要停止下载的场景中:
[timer invalidate];
用这个。 它可能会帮助你..
if (!timer)
{
timer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
}
在这里我添加摇动动画来指示下载进度。 最初的震动值是......
self.shake = 5;
self.direction = 1;
-(void)shake:(UILabel *)theOneYouWannaShake{
[UIView animateWithDuration:0.01 animations:^
{
theOneYouWannaShake.transform = CGAffineTransformMakeTranslation(5*self.directions, 0);
}
completion:^(BOOL finished)
{
if(self.shakes >= 5)
{
theOneYouWannaShake.transform = CGAffineTransformIdentity;
return;
}
self.shakes++;
self.directions = self.directions * -1;
[self shake:theOneYouWannaShake];
}];
}
- (void) updateLabel {
int status = self.count % 3;
if(status == 0)
{
label.text = @"Downloading."
}
else if(status == 1)
{
label.text = @"Downloading.."
}
else
{
label.text = @"Downloading...";
}
self.count += 1 ;
}
下载完成后。
[timer invalidate]
self.count = 0;
在viewDidLoad中:
self.downloadingLabel.text = @"downloading.";
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
然后添加方法:
- (void) tick:(NSTimer *) timer {
if ([self.downloadingLabel.text isEqual: @"downloading..."])
self.downloadingLabel.text = @"downloading.";
else
self.downloadingLabel.text = [self.downloadingLabel.text stringByAppendingString:@"."];
}
这将满足您的要求。 虽然您可能想要查看UIActivityIndicatorView等类;
我刚刚在2015年来到这个帖子,我对这里的答案不满意。 这是我的解决方案。 根据需要将4
更改为多个点。 这将做3个点( 4 - 1
)。 将“ Posting
”一词更改为您需要的任何状态。
首先添加如上所述的计时器。
self.ellipsisTimer = [NSTimer scheduledTimerWithTimeInterval:0.6
target:self
selector:@selector(updateLabelEllipsis:)
userInfo:nil
repeats:YES];
在您的更新方法中:
- (void) updateLabelEllipsis:(NSTimer *)timer {
NSString *messageText = [[self messageLabel] text];
NSInteger dotCount = messageText.length - [[messageText stringByReplacingOccurrencesOfString:@"." withString:@""] length] + 1;
[[self messageLabel] setText:@"Posting"];
NSString *addOn = @".";
if (dotCount < 4) {
addOn = [@"" stringByPaddingToLength:dotCount withString: @"." startingAtIndex:0];
}
[[self messageLabel] setText:[[[self messageLabel] text] stringByAppendingString:addOn]];
}
它的工作原理是计算当前的点数并再加一点。 一旦超过限制,它就会回到1点。
更新了Johnston对Swift的回答:
ellipsisTimer = NSTimer.scheduledTimerWithTimeInterval(0.6, target: self, selector: #selector(LinkCheckViewController.updateLabelEllipsis(_:)), userInfo: nil, repeats: true)
func updateLabelEllipsis(timer: NSTimer) {
let messageText: String = self.ThreeDotLabel.text!
let dotCount: Int = (ThreeDotLabel.text?.characters.count)! - messageText.stringByReplacingOccurrencesOfString(".", withString: "").characters.count + 1
self.ThreeDotLabel.text = " "
var addOn: String = "."
if dotCount < 4 {
addOn = "".stringByPaddingToLength(dotCount, withString: ".", startingAtIndex: 0)
}
self.ThreeDotLabel.text = self.ThreeDotLabel.text!.stringByAppendingString(addOn)
}
更改持续0.5秒的UILabel文本动画,在阵列中添加3个文本并一直更新,直到您的响应成功。
用户NSTimer用于更新UILabel文本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.