Reputation: 91
What I want is to animate my UILabel's text between 3 values repeatedly. The text I'd like to set is 'downloading.', 'downloading..', 'downloading...', and then, repeat. With this animation, I want to let my user know that the downloading is being done and the app is not stacked. I've been searching with google for a while and did not find a solution.
Any one can give me some reference about this? Thanks in advance.
Upvotes: 3
Views: 3705
Reputation: 6700
Updated Johnston 's answer for 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)
}
Upvotes: 2
Reputation: 20884
I just came to this post in 2015 and I wasn't satisfied with the answers here.
Here's my solution. Change the 4
for as many dots as you want. This will do 3 dots (4 - 1
). Change the word Posting
to whatever status you need.
First add a timer as stated above.
self.ellipsisTimer = [NSTimer scheduledTimerWithTimeInterval:0.6
target:self
selector:@selector(updateLabelEllipsis:)
userInfo:nil
repeats:YES];
In your update method:
- (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]];
}
It works by counting the current number of dots and adding one more. Once it goes above the limit it goes back to 1 dot.
Upvotes: 1
Reputation: 17595
Use this. It may help you..
if (!timer)
{
timer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
}
Here I add shake animation to indicate downloading progress. Initially the shake values are..
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 ;
}
After download complete.
[timer invalidate]
self.count = 0;
Upvotes: 1
Reputation: 2359
In viewDidLoad:
self.downloadingLabel.text = @"downloading.";
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
Then add the method:
- (void) tick:(NSTimer *) timer {
if ([self.downloadingLabel.text isEqual: @"downloading..."])
self.downloadingLabel.text = @"downloading.";
else
self.downloadingLabel.text = [self.downloadingLabel.text stringByAppendingString:@"."];
}
This will do what you asked for. Although you may want to look into classes like UIActivityIndicatorView;
Upvotes: 1
Reputation: 3089
for that you have to schedule the timer like this:
int count = 1;
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(updateLabel)
userInfo:nil
repeats:YES];
Now below method will get called in every (in seconds) periods
- (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;
}
}
whenever you want to stop Updating do this,in scenario you want to stop downloding :
[timer invalidate];
Upvotes: 5
Reputation: 9143
Use the NStimer for changing the text
in .h
file
@interface ViewController : UIViewController
{
NSTimer * timer;
UILabel * downloadingLbl;
}
and in .m
file
- (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.";
}
}
And when your download complete that make timer invalidate.
[timer invalidate];
Upvotes: 6
Reputation: 1542
Change the UILabel text animation of 0.5 seconds duration with adding 3 texts in array and updating all the time until your response succeed.
User NSTimer for updating UILabel text
Upvotes: 0