Monotouch:UILabel在t秒后消失,如何

发布于 2024-11-03 17:22:02 字数 573 浏览 3 评论 0原文

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

How is it possible to hide a UILabel after t seconds?

Could I use a background thread to do this?

Thank you in advance. Regards.

EDIT

For those interested in, following Luke advice:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半暖夏伤 2024-11-10 17:22:02

因为您要更改 UI,所以我建议使用主线程来实际隐藏标签,但是这是可能的:(

NSTimer timer  = NSTimer.CreateScheduledTimer(t, delegate{
    InvokeOnMainThread(delegate{
        label.Alpha = 0.0f;
    });
});

其中 t 是您想要隐藏标签的时间的 int!)

编辑
如果您想淡出标签,那么我建议您研究 UIView 动画。
在此处查看参考文档。从 iOS 4.0+ 开始,建议您使用 UIView 动画块。
为了适合您的示例,代码将如下所示:

NSTimer timer  = NSTimer.CreateScheduledTimer(t, delegate{
    InvokeOnMainThread(delegate{
        UIView.Animate(0.5f, delegate{
            label.Alpha = 0.0f;
        });
    });
});

第一个值是动画持续时间。
另请注意,在我使用这些委托{}的地方,您还可以执行以下操作:

NSTimer timer = NSTimer.CreateScheduledTimer(t, FadeLabelOut());

// later on

void FadeLabelOut()
{
    // do your stuff here
}

Because you'd be changing the UI, I would suggest using the Main Thread for the actual hiding of the label but yes this is possible:

NSTimer timer  = NSTimer.CreateScheduledTimer(t, delegate{
    InvokeOnMainThread(delegate{
        label.Alpha = 0.0f;
    });
});

(where t is an int for the time you'd like to hide the label!)

EDIT
If you want to fade the label out, then I'd suggest looking into UIView animations.
See reference docs here. As of iOS 4.0+ it is suggested you use UIView animation blocks.
To fit your example, the code would look like this:

NSTimer timer  = NSTimer.CreateScheduledTimer(t, delegate{
    InvokeOnMainThread(delegate{
        UIView.Animate(0.5f, delegate{
            label.Alpha = 0.0f;
        });
    });
});

The first value is the animation duration.
Just to note as well, where I'm using these delegate{} you can also do something like:

NSTimer timer = NSTimer.CreateScheduledTimer(t, FadeLabelOut());

// later on

void FadeLabelOut()
{
    // do your stuff here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文