[英]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.