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];
}
Related
i have a three buttons in home view controller . When i click but1 it perform animation and move toward right with a duration to a specific width. When but1 reach to its given width it hides and shows but2 there. when i hit but2 it perform animation and move back to the same position of but1. Its like a scenario of open and close of button. The issue now i'm facing is that when i open but1 it moves its given width and when i close it move back to original but1 position , now buttons open and close but when we try to open again it does not open with but1. I want that whenever user click but1 it should moves towards right with animation and every time user want to close the button it should close it rather than open or close only one time. My code for opening of button is this,
- (IBAction)openHome:(id)sender {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
CGAffineTransform transform = CGAffineTransformMakeTranslation(108, 0);
[_openHome setTransform:transform];
[UIView commitAnimations];
[NSTimer scheduledTimerWithTimeInterval:0.75
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:NO];
}
-(void)targetMethod:(NSTimer *)myTimer{
_home.hidden=NO;
_openHome.hidden=YES;
_closeHome.hidden=NO;
[myTimer invalidate];
}
For closing the button its this,
- (IBAction)closeHome:(id)sender {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
CGAffineTransform transform = CGAffineTransformMakeTranslation(-110, 0);
[_closeHome setTransform:transform];
[UIView commitAnimations];
[NSTimer scheduledTimerWithTimeInterval:0.75
target:self
selector:#selector(targetMethoded:)
userInfo:nil
repeats:NO];
}
-(void)targetMethoded:(NSTimer *)myTimer{
_home.hidden=YES;
_openHome.hidden=YES;
_closeHome.hidden=NO;
[myTimer invalidate];
}
The screen before open of button is this,
The screen after open of button,
Instead of using CGAffineTransform to change the position just change the buttons frame directly. Also use the following code which uses the completion handler to call the targetMethod instead of using the NSTimer:
[UIView animateWithDuration:0.75 animations:^{
_openHome.frame = CGRectMake(_openHome.frame.origin.x+110, _openHome.frame.origin.y, _openHome.frame.size.width, _openHome.frame.size.height);
} completion:^(BOOL finished) {
[self targetMethod];
}];
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 created some animations in my project. Basically, I use UIView animate and CGAffineTransform, but a very strange thing happened and I have no idea. Hope someone can help me solve this problem. Thanks in advance.
This is the strange thing:
After the user clicks on a button, the button slides off screen and another two buttons slide on the screen (I just changed the center point of these buttons to achieve this animation). And, some time later, a view on the screen start shaking (I use CGAffineTransform to achieve this).
At this moment, the strange thing happens - the button that previous slid off screen show up at its original position again and the other two buttons disappear (No animation, just shows up and disappear).
The following is the related code,
1) Button slide off and slide in animation related code
- (IBAction)start:(id)sender
{
// 1. Slide in cancel and pause button
[UIView animateWithDuration:0.5f animations:^{
[startButton setCenter:CGPointMake(startButton.center.x + 300.0f, startButton.center.y)];
[cancelButton setCenter:CGPointMake(cancelButton.center.x + 300.0f, cancelButton.center.y)];
[pauseButton setCenter:CGPointMake(pauseButton.center.x + 300.0f, pauseButton.center.y)];
} completion:^(BOOL finished) {
if (finished) {
NSLog(#"Move finished");
}
}];
}
2) The view shaking animation related code
- (void)shakeView:(UIView *)viewToShake
{
CGFloat t = 2.0;
CGAffineTransform translateRight = CGAffineTransformTranslate(CGAffineTransformIdentity, t, 0.0);
CGAffineTransform translateLeft = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, 0.0);
viewToShake.transform = translateLeft;
[UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{
[UIView setAnimationRepeatCount:2.0];
viewToShake.transform = translateRight;
} completion:^(BOOL finished) {
if (finished) {
[UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
viewToShake.transform = CGAffineTransformIdentity;
} completion:nil];
}
}];
}
This isn't weird, it's a common problem with
auto layout. If you move UI elements by changing frames, then as soon as something else takes place that requires laying out views, the moved views will revert to the position defined by their constraints. To fix it, you either need to turn off auto layout, or do your animations by changing constraints not frames.
I have tried your code in my test project. The problem is probably because you are using Autolayout in your xib.
Please checking your xib file and uncheck the Autolayout property.
I have a horizontally-scrolling paging UIScrollView in an iPad app, containing lots of pages. On the last page, I tap on a button on the screen to reset back to page 1. I would like to be able to cross-dissolve this transition, but it doesn't seem to work:
[UIView transitionWithView:self.view duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationOptionAllowAnimatedContent animations:^{
pagingScrollView.contentOffset = CGPointZero;
} completion:^(BOOL finished) {
[self refreshPages];
}];
I read that adding UIViewAnimationOptionAllowAnimatedContent will allow all content to transition, but it doesn't work. Instead, the screen cross-dissolves to the background colour, and when the transition is complete, the first page just appears.
you cannot fade-out a UIView (the scroller) AND simultaneously fade-in the same view...
you could just using different UIViews...
what you can do is:
1) fadeOut the scroller in the current position (to the backGround)
2) while the scroller is invisible, move it to the right position (with no animation)
3) fadeIn the scroller from the backGround
something like:
// START FIRST PART OF ANIMATION
[UIView animateWithDuration:0.5 delay:0.0 options: options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationOptionAllowAnimatedContent animations:^{
pagingScrollView.alpha = 0;
} completion:^(BOOL finished) {
// FIRST PART ENDED
// MOVE SCROLLER (no animation)
pagingScrollView.contentOffset = CGPointZero;
// START SECOND PART OF ANIMATION
[UIView animateWithDuration:0.5 delay:0.0 options: options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationOptionAllowAnimatedContent animations:^{
// fadeIn - animated
pagingScrollView.alpha = 1;
} completion:^(BOOL finished) {
// ANIMATION ENDED
[self refreshPages];
}];
}];
NEW EDIT:
thanks to amadour, who taught me something with his comments,
i hope he could add an answer of his own, i would vote for him
anyway, to answer to jowie original question:
i got the right animation just moving the contentOffset setting out of the animation block,
and removing UIViewAnimationOptionAllowAnimatedContent (not really needed), and passing pagingScrollView as parameter for transitionWithView
this worked for me:
pagingScrollView.contentOffset = CGPointZero;
[UIView transitionWithView:pagingScrollView duration:3.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
// pagingScrollView.contentOffset = CGPointZero; // move up, outside of animation block
} completion:^(BOOL finished) {
NSLog(#"-->> END amimation");
[self refreshPages];
}];
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];