刷过它后,Xamarin iOS更改Backgroundcolor

时间:2017-08-16 13:24:58

标签: xamarin xamarin.ios custom-renderer

我有一个带按钮的网格,如果用户滑过它们,它会改变背景颜色。 我创建了一个自定义Button类:

    public class TouchscreenTestButton : UIButton
{
    public override void TouchesBegan(NSSet touches, UIEvent evt)
    {
        base.TouchesBegan(touches, evt);
        this.BackgroundColor = UIColor.Green;

    }
    public override void TouchesMoved(NSSet touches, UIEvent evt)
    {
        base.TouchesMoved(touches, evt);
        UITouch touch = touches.AnyObject as UITouch;
        if (touch != null)
        {

            this.BackgroundColor = UIColor.Green;
            SetNeedsDisplay();
        }
    }
    public override void TouchesEnded(NSSet touches, UIEvent evt)
    {
        base.TouchesEnded(touches, evt);
        UITouch touch = touches.AnyObject as UITouch;
        if (touch != null)
        {
            this.BackgroundColor = UIColor.Green;
        }
    }
}

也是一个Button Renderer,用于将iOS的普通UIButtons替换为TouchscreenTestButton。

    public class TouchscreenTestButtonRenderer : ButtonRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
    {
        int columnIndex = Grid.GetColumn(e.NewElement);
        int rowIndex = Grid.GetRow(e.NewElement);
        TouchscreenTestButton button = new TouchscreenTestButton();
        button.TouchDown +=(sender, evt) => Control.BackgroundColor=UIColor.Green;

        base.SetNativeControl(button);

        base.OnElementChanged(e);
    }
}

这不起作用,只有我触摸的第一个按钮才会获得绿色背景色。之后我为网格创建一个渲染器:

    public class GridRenderer : ViewRenderer
{
    public override void TouchesMoved(NSSet touches, UIEvent evt)
    {
        base.TouchesMoved(touches, evt);
        var touch = touches.AnyObject as UITouch;
        for (int i = 0; i < touch.View.Subviews.Length; i++)
        {
            var test = touch.LocationInView(this);
            if (PointInside(touch.LocationInView(this), evt))
            {
                this.Subviews[i].TouchesBegan(touches, evt);
                this.Subviews[i].BackgroundColor = UIColor.Green;
                SetNeedsDisplay();
            }
        }
    }
}

但这也不起作用。我看到事件被触发,子视图的backgroundcolor将被更改,但视图不会更新。

可以请别人帮忙吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

我在我身边测试了以下代码,它有效。

[assembly: ExportRenderer(typeof(Grid), typeof(GridRenderer))]
namespace Slide.iOS
{
class GridRenderer :ViewRenderer
{

    public override void TouchesBegan(NSSet touches, UIEvent evt)
    {
        base.TouchesBegan(touches, evt);
        var touch = touches.AnyObject as UITouch;
        for (int i = 0; i < touch.View.Subviews.Length; i++)
        {
            var test = touch.LocationInView(this);
            var buttonView = touch.View.Subviews[i];

            CGRect frame = buttonView.Frame;

            if (frame.Contains(test))
            { 
                //pay attention
                var button = buttonView.Subviews[0];
                button.BackgroundColor = UIColor.Green;
            }
        }
    }

    public override void TouchesMoved(NSSet touches, UIEvent evt)
    {
        base.TouchesMoved(touches, evt);
        var touch = touches.AnyObject as UITouch;
        for (int i = 0; i < touch.View.Subviews.Length; i++)
        {
            var test = touch.LocationInView(this);
            var buttonView = touch.View.Subviews[i];

            CGRect frame = buttonView.Frame;

            if (frame.Contains(test)) {
                //pay attention
                var button = buttonView.Subviews[0];
                button.BackgroundColor = UIColor.Green;
            }
        }
    }
}

}

结果:

enter image description here

详细信息:

1.禁用Portable中的按钮,因为click事件将与Grid的触摸事件冲突。

<Button BackgroundColor="Red" Grid.Row="0" Grid.Column="0" IsEnabled="False"/>

2.确定您移动的点是否在该子视图(按钮)的框架中。

if (frame.Contains(test)) 

3。看看 buttonView ,注意它不是按钮!我在开始时设置了backgroundcolor,但它没有工作。当我们在Grid中布局控件时,它将根据其列和行自动生成子视图,然后将控件放在子视图上。

例如

<Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

上面的网格将有4个子视图,以及该子视图上的四个相应按钮。