我正在尝试使用CoreGraphics在monotouch上创建一个按钮。我无法弄清楚如何在推动时更改按钮颜色。我尝试重写TouchesBegan
和TouchesEnded
方法,但我无法弄清楚如何更改颜色。
答案 0 :(得分:5)
您可以通过两种方式处理背景颜色。您只需设置BackgroundColor属性即可。这应该会自动改变颜色。或者,如果要将颜色存储为要用于FillRect的私有变量,则需要调用SetNeedsDisplay强制调用Draw。
public override void TouchesBegan (MonoTouch.Foundation.NSSet touches, UIEvent evt)
{
base.TouchesBegan (touches, evt);
this.BackgroundColor = UIColor.Green;
backColor2 = UIColor.Yellow;
this.SetNeedsDisplay ();
}
public override void TouchesEnded (MonoTouch.Foundation.NSSet touches, UIEvent evt)
{
base.TouchesEnded (touches, evt);
this.BackgroundColor = UIColor.Red;
backColor2 = UIColor.Blue;
this.SetNeedsDisplay ();
}
public override void Draw (RectangleF rect)
{
base.Draw (rect);
using (CGContext context = UIGraphics.GetCurrentContext ()) {
RectangleF rect2 = this.Bounds;
rect2.X += 10;
rect2.Y += 5;
rect2.Width -= 20;
rect2.Height -= 10;
backColor2.SetFill ();
context.FillRect (rect2);
}
}