开发者

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 {
        isSho开发者_JAVA百科wing = NO;
        myView.hidden = NO;
                    //Needs to fade in here

}


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜