Xamarin iOS 在滑动后更改背景颜色
Xamarin iOS change Backgroundcolor after swipe over it
我有一个带按钮的网格,如果用户滑过它们,它会改变背景颜色。
我创建了一个自定义按钮 class:
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;
}
}
}
还有一个按钮渲染器,用于将 iOS 的普通 UIButton 替换为我的 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();
}
}
}
}
但这也行不通。我看到事件被触发,子视图的背景颜色将被更改,但视图不会更新。
有人可以帮忙吗?非常感谢!
我已经用下面的代码在我这边进行了测试,它有效。
[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;
}
}
}
}
}
结果:
详情:
1.DisablePortable中的按钮,因为click事件会和Grid中的touch事件冲突
<Button BackgroundColor="Red" Grid.Row="0" Grid.Column="0" IsEnabled="False"/>
2.Determine您移动到的点是否在该子视图(按钮)的框架中。
if (frame.Contains(test))
3。查看buttonView,注意不是按钮!!我一开始就在上面设置了背景色,但是没有用。当我们在 Grid 中布局控件时,它会根据它的列和行自动生成子视图,然后将控件放在子视图上。
例如
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
上面的网格将有 4 个子视图,以及子视图上的四个相应按钮。
我有一个带按钮的网格,如果用户滑过它们,它会改变背景颜色。 我创建了一个自定义按钮 class:
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;
}
}
}
还有一个按钮渲染器,用于将 iOS 的普通 UIButton 替换为我的 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();
}
}
}
}
但这也行不通。我看到事件被触发,子视图的背景颜色将被更改,但视图不会更新。
有人可以帮忙吗?非常感谢!
我已经用下面的代码在我这边进行了测试,它有效。
[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;
}
}
}
}
}
结果:
详情:
1.DisablePortable中的按钮,因为click事件会和Grid中的touch事件冲突
<Button BackgroundColor="Red" Grid.Row="0" Grid.Column="0" IsEnabled="False"/>
2.Determine您移动到的点是否在该子视图(按钮)的框架中。
if (frame.Contains(test))
3。查看buttonView,注意不是按钮!!我一开始就在上面设置了背景色,但是没有用。当我们在 Grid 中布局控件时,它会根据它的列和行自动生成子视图,然后将控件放在子视图上。
例如
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
上面的网格将有 4 个子视图,以及子视图上的四个相应按钮。