Krumelur
Krumelur

Reputation: 33068

Fading UILabel out, change text, fade back in: only fading in is animated

Simple task: I want to fade a UILabel out (alpha = 0.0), change the text, fade it back in (alpha = 1.0f) and all this animated. I figured out that fading out is not animated and I did a small test app.

If I run it I can see that the label disappears instantly when pressing the button and then fades back in. Why is the fading out ignored? I searched SO and I found code exactly like I'm using it.

What's wrong here? Do I have to split it up? If animation of fading out ends, create another one that fades back in?

UIButton btn = UIButton.FromType(UIButtonType.RoundedRect);
btn.Frame = new RectangleF(40, 40, 100, 30);

window.AddSubview(btn);

UILabel lbl = new UILabel(new RectangleF(100, 100, 100, 30));
lbl.Text = "A label";
window.AddSubview(lbl);
window.MakeKeyAndVisible ();

btn.TouchUpInside += delegate {
    UIView.Animate(1.0f, delegate {
        lbl.Alpha = 0.0f;
        lbl.Alpha = 1.0f;
    }, null);
};

Upvotes: 1

Views: 1256

Answers (1)

poupou
poupou

Reputation: 43553

I do not think you can animate the same property twice (inside the same delegate). There are other, more advanced, APIs to allow more complex animations but I think you can fake this one easily using:

btn.TouchUpInside += delegate {
    UIView.Animate (0.5f, delegate {
        lbl.Alpha = 0.0f;
    }, delegate {
        UIView.Animate (0.5f, delegate {
            lbl.Alpha = 1.0f;
        });
    });
};

which simply does the second part of your animation inside the completion action and splits the 1.0s time in two. YMMV

Upvotes: 2

Related Questions