如何在t秒后隐藏UILabel?
我可以使用后台线程来执行此操作吗?
提前谢谢你。问候。
修改
对于那些对卢克建议感兴趣的人:
var timer = NSTimer.CreateScheduledTimer(TimeSpan.FromSeconds(5), delegate{
InvokeOnMainThread(delegate{
UIView.BeginAnimations(null);
UIView.SetAnimationDuration(0.5);
UIView.SetAnimationTransition(UIViewAnimationTransition.None, labelToAnimateReference, true);
UIView.SetAnimationDelegate(this);
labelToAnimateReference.Alpha = 0.0f;
UIView.CommitAnimations();
});
});
答案 0 :(得分:2)
因为您要更改UI,我建议使用主线程来实际隐藏标签但是这是可能的:
NSTimer timer = NSTimer.CreateScheduledTimer(t, delegate{
InvokeOnMainThread(delegate{
label.Alpha = 0.0f;
});
});
(其中t是你想要隐藏标签的时间的int!)
修改强>
如果你想淡出标签,那么我建议调查UIView
动画。
See reference docs here。从iOS 4.0+开始,建议您使用UIView动画块。
为了适合您的示例,代码如下所示:
NSTimer timer = NSTimer.CreateScheduledTimer(t, delegate{
InvokeOnMainThread(delegate{
UIView.Animate(0.5f, delegate{
label.Alpha = 0.0f;
});
});
});
第一个值是动画持续时间。
还要注意的是,在我使用这些delegate{}
的地方,您也可以执行以下操作:
NSTimer timer = NSTimer.CreateScheduledTimer(t, FadeLabelOut());
// later on
void FadeLabelOut()
{
// do your stuff here
}