nosedive25
nosedive25

Reputation: 2435

Have an object fade in and out of existence

I have a view that sets its hidden when the user taps the main view. I need the view to fade in and out of existence so it looks smoother than just disappearing.

Heres my code so far (It's inside a touches event):

    if (!isShowing) {
        isShowing = YES;
        myView.hidden = YES;
                    //Needs to fade out here


}

    else {
        isShowing = NO;
        myView.hidden = NO;
                    //Needs to fade in here

}

Upvotes: 2

Views: 804

Answers (2)

David Dunham
David Dunham

Reputation: 8329

I've never had luck with animating hidden. Instead, animate alpha.

Upvotes: 6

Kristopher Johnson
Kristopher Johnson

Reputation: 82545

Just wrap your code like this:

[UIView beginAnimations:nil context:NULL];

if (!isShowing) {
    isShowing = YES;
    myView.hidden = NO
}
else {
    isShowing = NO;
    myView.hidden = YES
}

[UIView commitAnimations];

or simplify it to this:

[UIView beginAnimations:nil context:NULL];

isShowing = !isShowing;
myView.hidden = isShowing? NO : YES;

[UIView commitAnimations];

You might also want to use UIView's setAnimationDuration:, setAnimationCurve:, or setAnimationBeginsFromCurrentState: methods to customize how the view fades in and out.

Upvotes: 5

Related Questions