10

I've an array that includes 4 values which will be presented with 2 different UILabel. I'll group the values in pairs. In a specific second the label changes value with another item of pair recursively.

If it's possible, I want to give an animation something like fade out or maybe slide left/right effect.

I've looked some contents already, but nothing meant to me. For example here is a working animation (according to accepted answer):

[UIView animateWithDuration:1.0 delay:0.f options:(UIViewAnimationOptionAutoreverse| UIViewAnimationOptionRepeat) animations:^{ playerScore.alpha=1.f; } completion:^(BOOL finished){ playerScore.alpha=0.f; }]; 

I'm not sure where to put this code, in a seperate method or in viewDidLoad. Can anyone give clues and information? It would be great.

2 Answers 2

15

it is not clear for me why you need so many UILabel because you can add transition animations when i.e. you change the text in the current UILabel.

UILabel *textLabel = // ... whatever [textLabel setText:@"Initial text what I just write accidentally"]; CATransition *transitionAnimation = [CATransition animation]; [transitionAnimation setType:kCATransitionFade]; [transitionAnimation setDuration:0.3f]; [transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; [transitionAnimation setFillMode:kCAFillModeBoth]; [textLabel.layer addAnimation:transitionAnimation forKey:@"fadeAnimation"]; [textLabel setText:@"New text will show after a nice fade effect"]; 

it is very elegant and easy solution.


UPDATE (on 6 Jan 2016)

NOTE: the animation will be removed automatically after it is completed (or cancelled, or failed, or whatever), and you need to be aware of regenerating the animation for every session or keeping the current animation alive explicitly by setting the removedOnCompletion property to FALSE or NO.

Sign up to request clarification or add additional context in comments.

4 Comments

Holex, thank your for answer. I don't need so many UILabel, only 2 UILabel because I'll group my 4 values as pairs. In your snippet, the labels will change in every 0.3f or just one time?
@NewbieiOSDeveloper, it is for one time only and you can set the duration time to whatever you like.
I did as you instructed but I couldn't even realise animation. Also chaned duration still the same. Only "New text.." string is seen. What's wrong? I put these codes in my viewDidLoad. Another problem is, I need a animation cycle. Not only time.
@NewbieiOSDeveloper, the animations will be visible only if you perform them after the current view becomes visible, aka is in the navigation stack already. the animation in the actual view won't be visible if the actual view is not either.
1

Swift version of @holex answer:

textLabel.text = "originalValue" let textAnimation = CATransition() textAnimation.type = kCATransitionFade textAnimation.duration = 0.4 textLabel.layer.addAnimation(textAnimation, forKey: nil) textLabel.text = "newValue" 

If you'd like a callback for when the animation completes, use the following:

CATransaction.begin() CATransaction.setCompletionBlock({ print("Text Animation Complete!") }) let textAnimation = CATransition() textAnimation.type = kCATransitionFade textAnimation.duration = 0.4 textLabel.layer.addAnimation(textAnimation, forKey: nil) textLabel.text = "newValue" CATransaction.commit() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.