开发者

RemoveFromSuperView() with animation - AnimationWillEnd does not trigger

I have a UIView and try to remove it from its superview using an animation (fading to alpha 0.0). Works fine but the view is never removed from the superview although I added a delegate to An开发者_JS百科imationWillEnd. Here's the code. The console output is not written and the view is not removed. What's wrong?

    UIButton oBtn = UIButton.FromType(UIButtonType.RoundedRect);
    oBtn.Frame = new RectangleF(0, 0, 100, 20);
    oBtn.SetTitle("Hide", UIControlState.Normal);
    oBtn.Center = new PointF(80, 120);
    oBtn.TouchUpInside += delegate(object sender, EventArgs e) {
        UIView.BeginAnimations(null);

            UIView.AnimationWillEnd += delegate {
           Console.WriteLine("Removed.");
           oView.RemoveFromSuperview();
            };

    UIView.SetAnimationDuration(2);
    UIView.SetAnimationBeginsFromCurrentState(true);
    oView.Alpha = 0.0f;
    UIView.CommitAnimations();
   };
   oView.AddSubview(oBtn);


I tried a lot of things with your code but it seems that the UIView.AnimationWillEnd handler never gets called. There is a way however, to perform the task you want:

oBtn.TouchUpInside += delegate(object sender, EventArgs e) {

        UIView.Animate(2, 
            delegate {
                oView.Alpha = 0.0f;
            },
            delegate {
                Console.WriteLine("Removed.");
                oView.RemoveFromSuperview();
            });
   };

The second anonymous method gets called when the animation completes. You can check Animate's other overloads for more options.


The button needs to be defined outside of the scope of the method, otherwise it will get collected sooner than expected. Try defining the button at the class level, and then setting it to null in your AnimationWillEnd delegate.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜