I have an UIImage and I want to set its alpha at 0.00 after 3 seconds... In my UIImage there is a picture that I want to immediately see when I go in a view, but after 3 second it must disappear.
Easy as that using core animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3.0f];
imageView.alpha = 0.0f;
[UIView commitAnimations];
if you dont want to slowly fade within 3 seconds, you can use
[UIView setAnimationDelay:3];
and reduce the animation duraction to 0.5f or something.
i think using a short fade out time feels better than just simply set hide to YES
This will make it fade out within three seconds. Call from viewDidLoad:
[UIView animateWithDuration:3.0 animations:^{myImage.alpha = 0; }];
Or if you want the animation to start at 2.5 seconds and last half a second, you could put that into a method (changing 3.0 to 0.5) and then call:
[NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:#selector(hideMyImage) userInfo:nil repeats:no];
Are you showing your UIImage in a UIImageView?
If so, just set the hidden property on that image view to YES and the image view (with its image) will disappear.
You can always remove the sub-child view from the parent.
[imageView removeFromSuperview];
you can always add it later with
[self.view addSubview:imageView];
Related
I am trying to fade one view in while fading the other out using animateWithDuration and changing the alpha values of each. The problem I'm having is that the views seem to fade through a black background. I would like to keep the transitioning background a white color being that both of my views have white backgrounds.
How can I fade between my two views without transitioning through a black background?
Here's my code:
[self.view1 setAlpha:0.0];
[self.view addSubview:self.view1];
[UIView animateWithDuration:0.5
animations:^{self.view1.alpha = 1.0;self.view2.alpha = 0.0;}
completion:^(BOOL finished){[self.view2 removeFromSuperview];}];
How about something like this:
[UIView transitionWithView:self.view
duration:0.4
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self.view addSubview:self.view1];
}
completion:^(BOOL finished) {
[self.view2 removeFromSuperview];
}];
Use: animateWithDuration:animations:completion:
Set the new image view alpha to 0.
Place the new image view in front of the current image view.
In animations: set alpha of the new image view to 1.0.
In completion: handler cleanup how ever you want.
I'm using [UIView transitionWithView:] for animation.
This method don't have delay only duration, but I need to have some delay.
For example: Need to change 1 image to another with duration 1 sec, then after 4 second change image to another with duration 1 second.
I know about [UIView animateWithDuration:] that have delay but i don't change frame it don't change image for more then 1 time.
I use next:
[UIView transitionWithView:self.view
duration: 1.0f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
//here change image
} completion:^(BOOL finished){
[NSThread sleepForTimeInterval: 4.0f];
}];
It's work fine. But it I change view ( I have scrollView and added a lot of view with animation) it show another view and it's own animation but [NSThread sleepForTimeInterval: 4.0f]; from previous page work on it's to.
So how I can make delay for my animation?
Thanks for help!
I personally like to rely on timers. They don't hang the app and give you more flexibility to control your triggers.
NSTimer *mytimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(myAnimationFunction:) userInfo:nil repeats:NO];
-(void) myAnimationFunction:(NSTimer *)timer{<br>
// Do anything here...<br>
}
I am trying to create a Calibration View! I have 12 calibration points as UIImageViews. Now I want to show every point for 5 seconds. So the whole calibration time is 1 minute. The Alpha of the ImageViews is set to 0.0! In the UI Animation I want to set the Alpha to 1.0 for one point after an other, for each point only for 5 seconds.
So far I did this (see the code)! But this animates me (fades in) after 5 seconds for 5 seconds all 12 Points at once!How can I solve this for one after another with a NSTimer and UI Animation? Thanks
-(id)init{
_calibrationViewArray = [[NSMutableArray alloc]init];
_calibrationPoint1 = [[UIImageView alloc]initWithFrame:(CGRectMake(115.5,113.0,25.0,25.0))];
_calibrationPoint1.backgroundColor = [UIColor redColor];
_calibrationPoint1.alpha = 0.0;
[self addSubview:_calibrationPoint1];
[_calibrationViewArray addObject:_calibrationPoint1];
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
}
-(void)onTimer{
for (int i = 0; i < [_calibrationViewArray count]; i++) {
UIImageView* currentCalibrationPoint = [_calibrationViewArray objectAtIndex:i];
[UIView beginAnimations:#"Calibration" context:nil];
[UIView setAnimationDuration:5.0];
// Make the animatable changes.
currentCalibrationPoint.alpha = 1.0;
// Commit the changes and perform the animation.
[UIView commitAnimations];
}
}
Declare a variable in your class to be:
int cont;
Initialize variable when is needed:
cont=0;
Change your code to this:
-(void)onTimer{
//Loop is not needed, with the timer and counter is enough
UIImageView* currentCalibrationPoint = [_calibrationViewArray objectAtIndex:cont];
[UIView beginAnimations:#"Calibration" context:nil];
[UIView setAnimationDuration:5.0];
// Make the animatable changes.
currentCalibrationPoint.alpha = 1.0;
// Commit the changes and perform the animation.
[UIView commitAnimations];
if(cont>0){
//Hide previous view (here you can make another animation, instead of changing alpha right away)
[_calibrationViewArray objectAtIndex:cont-1].alpha = 0.0;
}
cont++;//+1
}
In your onTimer method you shouldn't be looping over all of the image views, you should have a currentIndex or similar variable so you can just get the next image view and run an animation on it alone.
Do not use timer at all.
Use the:
[UIView animateWithDuration: animations: completion:]
method.
In the animation block fade one of the dots, then in completion block run the animation again.
I've been reading about UIView animateWithDuration which I'm trying to use so when a button is pressed a graphic appears then slowly fades out (i.e. alpha is set to 0).
I'm using the code below in my viewdidload just for test purposes however its not working:
[UIView animateWithDuration:10 animations:^{
self.completeImage.alpha = 1.0;
self.completeImage.alpha = 0.5;
self.completeImage.alpha = 0.0;
}];
Any ideas?
Thanks.
That is not working because automatically it sets the alpha to 0.0;
The 3 lines of code are executed at the same time (one after the other).
The proper way to use the UView animation block it is like this:
self.completeImage.alpha = 0.0;
[UIView animateWithDuration:2.0
animations:^{
// do first animation
self.completeImage.alpha = 1.0;
}
completion:^(BOOL finished){
[UIView animateWithDuration:2.0
animations:^{
// do second animation
self.completeImage.alpha = 0.0;
}
completion:^(BOOL finished){
;
}];
}];
Hope this achieve what you are looking for.
In addition:
" I'm trying to use so when a button is pressed a graphic appears
then slowly fades out (i.e. alpha is set to 0)."
As per your above information in the question, addition of the code in viewDidLoad will not prove fruitful. You need to add this code in the action target method of your button in order to play the animation on click of a button. Generally if you're using the nib, then the action method will be like below:
-(IBAction)on_pressing_my_button:(id)sender
{
///your animation code goes here..
}
I want to make a button fade away after it is clicked. I know that I can use
_myButon.hidden = TRUE;
...to completely hide a button, but it feels abrupt and jarring. I also know I could sequentially lower the alpha or something, but wasn't sure how to make this happen automatically over a short period of time.
Can someone please give me a tip how to fade out a button after it is clicked using the simplest means possible? I want the effect to look something like a simple "Fade-out" from a powerpoint presentation or something :)
Thanks!
[UIView animateWithDuration:0.5 animations:^{
_myButton.alpha = 0;
}];
Rather than removing the button, just hide it. Taking all the suggestions into account you get:
[UIView animateWithDuration:0.5
animations:^{ _myButton.alpha = 0; }
completion:^(BOOL finished){ _myButton.hidden = YES; }
];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
_myButton.alpha = 0.0f;
[UIView commitAnimations];
alternatively
[UIView animateWithDuration:1.0 animations:^{
_myButton.alpha = 0.0f;
}];
Just reducing the alpha is not going to make your button completely be removed from your view. To a user it will look like its gone but its still there. They potentially still could accidently click it without knowing. So what you can do is do a timer to remove it from view after it has faded away.
...
//alpha animation
//remove from view
timer1 = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:#selector(hideMyButton) userInfo:nil repeats:NO];
}
-(IBAction) hideMyButton
{
[_myButon removeFromSuperview];
}