Nico
Nico

Reputation: 6369

How to animate text inside a label from right to left as a new entry comes up?

I have a calculator-like with UIButtons for the digits (0 to 9) and one UILabel to display the user's inputs.

I would like that for each input, the digits already in the label moves on the left with a slide animation and the new digit appear from the bottom with an animation. How can I do that?

I tried with a CATransition with kCATransitionMoveIn type but it moves the all label (which makes probably sense as it works on the layer level).

But how can I do it if I just want to move the content (the text) and not the frame or layer?

Upvotes: 0

Views: 3168

Answers (2)

Dennis
Dennis

Reputation: 2175

It is possible to animate the adding of new digits. However, you have to play around with the animation type a bit to achieve a sliding instead of a fading. But this should give you a start:

- (void)addDigit:(NSString *)digit {

    // Add the transition (this must be called after self.calcLabel has been displayed)
    CATransition *animation = [CATransition animation];
    animation.duration = 1.0;
    animation.type = kCATransitionFromRight;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.calcLabel.layer addAnimation:animation forKey:@"changeTextTransition"];

    // Change the actual text value
    NSString *currentText = self.calcLabel.text;
    NSString *newText = [NSString stringWithFormat:@"%@%@", currentText, digit];
    self.calcLabel.text = newText;
}

Credits to https://stackoverflow.com/a/6267259/1770453

EDIT

I hope this is not the code you already have.

Upvotes: 0

Burhanuddin Sunelwala
Burhanuddin Sunelwala

Reputation: 5343

Well for a simple solution you need to create multiple labels and animate them. From multiple labels I mean labels for each digit or symbol. You dynamically create these and animate accordingly.

If you want to only use one label and animate the inside text content then that would not be possible because no such APIs are provided by UILabel.

You can create your own custom Label by using CoreText. I personally don't know much about it but you can read on it.

https://developer.apple.com/library/mac/documentation/StringsTextFonts/Conceptual/CoreText_Programming/Introduction/Introduction.html

But my personal opinion would be to use labels for each digit. It would be good to say rather that consider this each label as a text (digit).

Hope this helps

Upvotes: 3

Related Questions