views:

176

answers:

2

Hello,

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

}
+1  A: 

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.

Kristopher Johnson
Hmmmm... couldn't get it to work. Do I need to replace UIView with something? Im still new to cocoa.
happyCoding25
UIView is the class that has the methods. Does the view never show up at all? Are you sure it exists and is a subview of whatever view is on screen?
Kristopher Johnson
Yes the view does show up and it is a subview of the main view. I tried doing [UIView setAnimationDuration:5.0]; but no luck.
happyCoding25
Got it to work, thanks for the help.
happyCoding25
Did animating `hidden` work? Now that I look into it, I think you maybe you need to animate the `alpha` property.
Kristopher Johnson
I did switch it to alpha and then ran into another issue. The hidden was hiding it before it could animate out so I just removed hidden and used alpha.
happyCoding25
+3  A: 

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

David Dunham
That is exactly how i do it. Take the .alpha property from 1 to 0 to hide and from 0 to 1 to show. Remember to wrap them in the beginAnimation and commitAnimation blocks.
Jann