Xamarin IOS 暂时更改按钮的颜色 [英] Xamarin IOS Change color of button momentarily

查看:18
本文介绍了Xamarin IOS 暂时更改按钮的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个测验应用程序.用户选择一个答案,在自动被引导到下一个问题之前得到一个简短的确认答案是正确还是错误.所以,我有一个视图,它有一个标签和 4 个不同的按钮,不同的文本来自数据库.当用户选择一个按钮时,在按钮加载不同的文本之前,它应该闪烁或具有不同的背景颜色几秒钟.

I'm creating a quiz application. The user selects an answer, get a brief confirmation whether the answer is correct or wrong before automatically is directed to the next question. So, I have a view that has a label and 4 different buttons with different texts coming from database. When the user selects a button, it should flash or have a different background color for few seconds before the buttons are loaded with different text.

我曾尝试在 touchUpInside 中执行此操作.我为按钮设置了不同的颜色,然后执行 Thread.Sleep(2000).这是我对其中一个按钮的 TouchUpInside 的事件处理程序:

I have tried doing this in the touchUpInside. I set a different color for the button and I do Thread.Sleep(2000). This is my event handler of the TouchUpInside of one of the buttons:

    partial void Answer(MonoTouch.Foundation.NSObject sender)
    {
        // if wrong answer, highlight to red and move on.
        ((UIButton)sender).backgroundColor = UIColor.Red;
        Sleep(2000); // freeze to display the red color for 2 seconds
        LoadNextQuestion(); // load the next question method
    }

但是颜色只有在下一个问题加载后才会改变.我怎样才能让它在那里冻结 2 秒?

But the color only changes after the next question loads. How can I make it freeze there for 2 seconds?

谢谢

推荐答案

你可以用动画来做到这一点:

You can do this with an animation:

button1.TouchDown += delegate(object sender, EventArgs e) {
  UIView.Animate(2,0,UIViewAnimationOptions.Autoreverse,
    () => { button2.BackgroundColor = UIColor.Yellow; }, 
    () => { button2.BackgroundColor = UIColor.Green; });
};

这里 button1 正在触发 button2 的动画,但您可以将触发器设置为任何内容.此示例仅将按钮的背景颜色从绿色变为黄色,然后再次返回动画,持续时间为 2 秒.

Here button1 is triggering the animation of button2, but you could setup the trigger to be anything. This sample just animates the button's background color from Green to Yellow and back Again, with a 2s duration.

这篇关于Xamarin IOS 暂时更改按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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