带动画的RemoveFromSuperView() - 不会触发AnimationWillEnd

Kru*_*lur 2 xamarin.ios

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

    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);
Run Code Online (Sandbox Code Playgroud)

Dim*_*kos 5

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

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

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

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

  • 啊甜蜜,我不知道你可以只提供代表作为NSAction (2认同)