Here is some code I struggle with for a while.
If you start the fade in animation, the label text fades in. If I start the fade out animation the the label text fades out.
When I start the startFade
method, only fade out is shown. How can I wait for the fadeIn
method to finish visually before starting the fadeOut
method.
-(IBAction)startFade:(id)sender{
[self fadeIn];
[self fadeOut];
}
-(IBAction)fadeIn:(id)sender{
[self fadeIn];
}
-(IBAction)fadeOut:(id)sender{
[self fadeOut];
}
-(void) fadeIn{
[_label setAlpha:0];
[UILabel beginAnimations:NULL context:nil];
[UILabel setAnimationDuration:2.0];
[_label setAlpha:1];
[UILabel commitAnimations];
}
-(void) fadeOut{
[UILabel beginAnimations:NULL context:nil];
[UILabel setAnimationDuration:2.0];
[_label setAlpha:0];
[UILabel commitAnimations];
}
you can do something like this (check possible parameters values and similar methods here : https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html
The easiest way would be to use:
and add the
fadeOut
call to thecompletion
block. The documentation might help answer any questions you have.If you can't use the block version for some reason, then you'll have to set a delegate (
[UIView setAnimationDelegate:(id)delegate]
) and a selector with ([UIView setAnimationDidStopSelector:]
) that the delegate will respond to.Again, see the documentation for more details.
Try this..
// Fade Out
// Fade In
// Fade in from fade out
// Button operation
View this link to download sample..
Refer this link.
Happy to share with you..:-)
Swift 5 version of iPhoneDeveloper's answer:
}
Generic answer : You can use this method to apply animation to any UIView object . First create an extension of UIView class . Create a separate swift file and write the code like this
Here self refers to any UIView you refer to . You can use buttons, labels etc to call these 2 methods .
Then in any other swift class you can call fadeIn() and fadeOut() like this :
This gives the desired effect of animation to any UIObject .
Swift 4 If you need just one pulse when clicking the button, use that: