我有一个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);
答案 0 :(得分:5)
我用你的代码尝试了很多东西,但似乎永远不会调用UIView.AnimationWillEnd处理程序。但是,有一种方法可以执行您想要的任务:
oBtn.TouchUpInside += delegate(object sender, EventArgs e) {
UIView.Animate(2,
delegate {
oView.Alpha = 0.0f;
},
delegate {
Console.WriteLine("Removed.");
oView.RemoveFromSuperview();
});
};
动画完成时会调用第二个匿名方法。您可以检查Animate的其他重载以获得更多选项。
答案 1 :(得分:0)
按钮需要在方法范围之外定义,否则将比预期更早收集。尝试在类级别定义按钮,然后在AnimationWillEnd委托中将其设置为null。