How do I have text fade in to a UILabel? - ios

Right now I have an IBAction for a button which generates a random number with each number assigned to display text in a UILabel. Rather than just appear, I would like the text to fade in or any other interesting animation would be cool too. Does anyone know a simple way to make this happen?
Right now I just use
labelMain.text = #"my text";

I know this is a bit stale (11 months old), but I think it's worth mentioning an alternative to the other approaches posted, which I feel is a better solution.
Make use of the UIView transitionWithView:duration:options:animations:completion class-level method, like this:
NSTimeInterval duration = 0.5f;
[UIView transitionWithView:labelMain
duration:duration
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
labelMain.text = #"new value";
} completion:nil];
This will cross-fade from the previous value of labelMain.text to "new value".
This approach works on other view values as well, such as changing the image of a UIImageView, for instance.
See Apple's docs on the topic.

The following code will give you fading effect on your label.
- (IBAction)buttonClicked:(UIButton *)sender
{
labelMain.alpha = 0;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseIn
animations:^{ labelMain.alpha = 1;}
completion:nil];
}

label.text = #"old text";
[UIView animateWithDuration:0.4 animations:^{
label.alpha = 0.0f;
} completion:^(BOOL finished) {
label.text = #"next text";
[UIView animateWithDuration:0.4 animations:^{
label.alpha = 1.0f;
} completion:^(BOOL finished) {
NSLog(#"finished transition");
}];
}];

Try this:
[labelMain setAlpha:0];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8];
[labelMain setAlpha:1];
[UIView commitAnimations];
Changing the duration is pretty simple - just adjust the value 0.8 in the code.

// fade out the current value
[UIView animateWithDuration:0.2
delay:0
options:0
animations:^{
labelMain.alpha = 0.0f;
}
completion:^(BOOL finished) {
// set the new value and fade in
labelMain.text = newValue;
[UIView animateWithDuration:0.2
delay:0
options:0
animations:^{
labelMain.alpha = 1.0f;
}
completion:nil];
}];

This will look like cross fade animation
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut
animations:^{
labelMain.alpha = 0;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseIn
animations:^{ labelMain.alpha = 1;}
completion:nil];
} completion:nil];

Related

Fade In / Fade out 2 Labels infinite loop inside tableview

I'm trying to show an intermittent label with two changing strings (like a banner) in an infinite loop.
The first label should fade in. After that it should fade out and let a second label do the same. Then repeat the sequence infinitely or a long time.
The label is inside a subclass of a UITableView.
I tried this so far:
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
// Inside a cell an at its own section of the tableview
cell.label1.alpha = 1;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationRepeatCount:INFINITY];
[cell.label1 setAlpha:0];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView commitAnimations];
cell.label2.alpha = 1;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationRepeatCount:INFINITY];
[cell.label1 setAlpha:0];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView commitAnimations];
...
}
This code works fine but the effect is not clear because the fade in label1 is crossing fade out label2 and it is weird.
So, how can I make a break between the two animations, to see the fading labels clearly?
Thanks #Paulw11 for your answer, now it works fine, here is my code with the changes:
cell.label2.alpha = 0;
cell.label1.alpha = 1;
[UIView animateKeyframesWithDuration:15 delay:0.0 options:0 animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.25 animations:^{
cell.label1.alpha = 0;
cell.label2.alpha = 0;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:0.25 relativeDuration:0.25 animations:^{
cell.label1.alpha = 0;
cell.label2.alpha = 1;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:0.50 relativeDuration:0.25 animations:^{
cell.label1.alpha = 0;
cell.label2.alpha = 0;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:0.75 relativeDuration:0.25 animations:^{
cell.label1.alpha = 1;
cell.label2.alpha = 0;
[self.view layoutIfNeeded];
}];
[UIView setAnimationRepeatCount: 200];
} completion:nil];

Animation is happening in reverse

I'm making a simple animation where when a button is tapped it will change from current position to a certain point. But what's happening is the opposite: it goes from the destination point to the original position. Here's my code:
[UIView animateWithDuration:1.0
delay:0.0
usingSpringWithDamping:0.2
initialSpringVelocity:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[self.view layoutIfNeeded];
CGFloat amount = 100;
self.checkButton.frame = CGRectOffset(self.checkButton.frame, amount, 0);
} completion:^(BOOL finished) {
}];
Make an outlet to the constraint that determine the horizontal position of the checkButton. e.g."hPos".
- (IBAction)tapHandler:(id)sender {
self.hPos.constant = self.hPos.constant + 100;
[UIView animateWithDuration:1.0
delay:0.0
usingSpringWithDamping:0.2
initialSpringVelocity:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{ [self.view layoutIfNeeded]; }
completion:nil];
}

Recursive animateWithDuration: animations: completion: method iOS

I am attempting to make my item "glow" every 3 seconds changing its alpha from a high to low value and back. It however "flickers" and basically changes every 0.2 seconds from color to color randomly. I have pasted the method below. Its probably something to do with when completion is called but not sure?
-(void)showLoading{
[self.postTableView setHidden:YES];
self.isLoading = true;
//Disappear
[UIView animateWithDuration:1.5 animations:^(void) {
self.loadingLabel.alpha = 0.8;
self.loadingLabel.alpha = 0.3;
}
completion:^(BOOL finished){
//Appear
[UIView animateWithDuration:1.5 animations:^(void) {
self.loadingLabel.alpha = 0.3;
self.loadingLabel.alpha = 0.8;
}completion:^(BOOL finished2){
if(true){
[self showLoading];
}
}];
}]; }
self.loadingLabel.alpha = .8;
[UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
self.loadingLabel.alpha = .3;
} completion:nil];
UIViewAnimationOptionAutoreverse & UIViewAnimationOptionRepeat can help you.
you can stop it by
self.loadingLabel.alpha = .8;
or the other animation.
[UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.loadingLabel.alpha = .8;
} completion:nil];
UIViewAnimationOptionBeginFromCurrentState is a useful option for you to stop your animation by the other animation.
-(void)viewDidLoad{
//use schedule timer to call repeatedly showLoading method until loading done
}
-(void)showLoading{
[self.postTableView setHidden:YES];
self.isLoading = true;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
self.loadingLabel.alpha = 0.3;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
self.loadingLabel.alpha = 0.8;
[UIView commitAnimations];
}

Different animation duration for subviews of an UIView

I have the following code
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
mainPage.frame=CGRectMake(0, -[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height,[UIScreen mainScreen].bounds.size.width);
} completion:nil];
Is there any way to control the animation duration of the subviews of mainPage.
You can use UIView:animateKeyframesWithDuration then use UIView:addKeyframeWithRelativeStartTime to set its start time and duration
[UIView animateKeyframesWithDuration:2 delay:0 options:0 animations:^{
[UIView addKeyframeWithRelativeStartTime:0.32 relativeDuration:0.68 animations:^{
view.frame = frame;
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:1 animations:^{
subview.frame = frame2;
}];
} completion:^(BOOL finished) {
}];
If you want the subviews to animate with a different duration then you'll need to animate each of them separately. You can loop through the mainPage.subviews array and issue an animateWithDuration:delay:options:animations:completion: call to each subview in turn, each with a different duration.

animateWithDuration not recognised?

I'm trying to do a cross dissolve on three UILabels (display1, display2, display3), by using a block animation to fade out, change text and then fade them in one at a time. The code I'm using is:
[UIView animateWithDuration:1.0 delay: 1.0
animations:^{
display1.alpha = 0.0;
display2.alpha = 0.0;
display3.alpha = 0.0;
}
completion:^{
[display1 setText:[NSString stringWithFormat:#"%#",[engine getstring]]];
[display2 setText:[NSString stringWithFormat:#"%#",[engine getstring]]];
[display3 setText:[NSString stringWithFormat:#"%#",[engine getstring]]];
[UIView animateWithDuration:1.0 delay:1.0
animations:^{
display1.alpha = 1.0;
[UIView animateWithDuration:1.0 delay:1.0
animations:^{
display2.alpha = 1.0;
[UIView animateWithDuration:1.0
delay:1.0
animations:^{
display3.alpha = 1.0;
} completion:nil];
} completion:nil];
} completion:nil];
}];
I get the following warnings:
Method '+animateWithDuration:delay:animations:completion:' not found*
and
'UIView' may not respond to method '+animateWithDuration:delay:animations:completion:'
I'm using Xcode 4.0 with an iOS Build Target of 4.3.
You want to use
+ animateWithDuration:delay:options:animations:completion:
instead. Note the options: in the middle.

Resources