我想要的是重复为3个值之间的UILabel文本设置动画。我要设置的文字是“下载”,“下载..”,“下载...”,然后重复。有了这个动画,我想让我的用户知道正在完成下载并且应用程序没有堆叠。我一直在谷歌搜索一段时间,但没有找到解决方案。
任何人都可以给我一些参考吗?提前谢谢。
答案 0 :(得分:6)
使用NStimer更改文本
<{1>}文件中的
.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.";
}
}
答案 1 :(得分:5)
为此你必须像这样安排计时器:
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];
答案 2 :(得分:1)
使用它。它可能对你有帮助..
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;
答案 3 :(得分:1)
在 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等类;
答案 4 :(得分:1)
我刚刚在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点。
答案 5 :(得分: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)
}
答案 6 :(得分:0)
更改持续0.5秒的UILabel文本动画,在阵列中添加3个文本并一直更新,直到您的响应成功。
用户NSTimer用于更新UILabel文本