XandruCea XandruCea - 3 months ago 31
iOS Question

How can I add characters to an UILabel animated in iOS with Objective-C?

What I tried

I created a recursive function that loops over a

NSMutableArray
and ever time will go through this animation blog. This method is called by this code because I thought the problem why the animation on the
UILabel
doesn't work is, that it is not run on the main thread.

[self performSelectorOnMainThread:@selector(writeText:)
withObject:_arrayAllLabels
waitUntilDone:YES];


The animation has a
completion
block which also checks whether the animation is finished and then moves on to call the same function again. Furthermore I added an additional animation blog but nothing makes it able to add characters to the label smooth.

[UIView animateWithDuration:1.5f
delay:1.0f
options:UIViewAnimationOptionCurveEaseOut
animations:^{

//adding next character to text attribute of UILabel
} completion:^(BOOL finished) {

[UIView animateWithDuration:1.0f
animations:^{

if ( finished ){
[self performSelectorOnMainThread:@selector(writeText:)
withObject:_arrayAllLabels
waitUntilDone:YES];

}
} completion:nil];

}];


What It looks like

enter image description here

What I found after my search wasn't right

I have been looking around for a while now and tried to find a question related to my problem but everything I found pointed only into one direction, changing the text of an
UILabel
to another text, no single character


  • stackoverflow.com/questions/26402062/changing-the-text-of-a-uilabel-makes-it-reappear-when-using-uiviewkeyframeanimat

  • stackoverflow.com/questions/26703501/fade-between-text-in-a-uilabel-with-delay



Can Somebody help?

Answer

the setText method of a UILabel is not animatable. animateWithDuration:completion: will only animate change in frames, bounds, center, transform... See Apple docs on animation here for the list of animatable property : https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/AnimatingViews/AnimatingViews.html

For your problem, you have to forget using animateWithDuration:completion: and create the animation yourself, by using a NSTimer for example for executing a addLetter: method every x seconds...