带动画的 RemoveFromSuperView() - AnimationWillEnd 不会触发 [英] RemoveFromSuperView() with animation - AnimationWillEnd does not trigger

查看:25
本文介绍了带动画的 RemoveFromSuperView() - AnimationWillEnd 不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIView 并尝试使用动画(淡入 alpha 0.0)将其从其超级视图中删除.工作正常,但视图从未从超级视图中删除,尽管我向 AnimationWillEnd 添加了一个委托.这是代码.控制台输出不会被写入,视图也不会被移除.怎么了?

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 AnimationWillEnd. 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);

推荐答案

我用你的代码尝试了很多东西,但似乎 UIView.AnimationWillEnd 处理程序永远不会被调用.但是,有一种方法可以执行您想要的任务:

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();
            });
   };

第二个匿名方法在动画完成时被调用.您可以查看 Animate 的其他重载以获取更多选项.

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

这篇关于带动画的 RemoveFromSuperView() - AnimationWillEnd 不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆