jake9115
jake9115

Reputation: 4084

How to make a button fade after clicked for iOS / xcode?

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!

Upvotes: 4

Views: 4142

Answers (4)

Gary Makin
Gary Makin

Reputation: 3169

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; }
];

Upvotes: 4

Sam B
Sam B

Reputation: 27618

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];
    }

Upvotes: 1

Khanh Nguyen
Khanh Nguyen

Reputation: 11132

[UIView animateWithDuration:0.5 animations:^{
    _myButton.alpha = 0;
}];

Upvotes: 8

esqew
esqew

Reputation: 44710

[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;
}];

Upvotes: 2

Related Questions