使用适用于 iOS 的 .net8 MAUI,我在 iOS 应用程序中显示图像。它有一些“热区”,是看不见的按钮。他们会触发某种行动(例如提高分数)。
我想在用户点击的位置显示一种特殊的“鼠标光标”(我将使用 .png )。 我可以使用 AbsoluteLayout、使用 AbsoluteLayout.LayoutBounds 在任何我想要的地方显示光标(png-image)。但是,我无法在 C# 中动态更新此位置。 对此有什么想法吗?
Cursor myCursor = new Cursor("custom.cur"); --> 没有成功,需要 Cursor 类。
我认为你可以简单地使用GestureRecognizers。我已经在 Android 上尝试过,但它应该以同样的方式在 ios 上工作:
xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ImageTap.MainPage"
Shell.NavBarIsVisible="False">
<AbsoluteLayout>
<VerticalStackLayout
BackgroundColor="Blue" AbsoluteLayout.LayoutBounds="0,0,1000,1000">
<VerticalStackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" NumberOfTapsRequired="1" />
</VerticalStackLayout.GestureRecognizers>
</VerticalStackLayout>
<Image x:Name="cursor" Source="cursor.png" AbsoluteLayout.LayoutBounds="0,0,128,128"></Image>
</AbsoluteLayout>
</ContentPage>
cs:
namespace ImageTap
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
{
Point? clickedPosition = e.GetPosition((View)sender);
cursor.TranslationX = clickedPosition.Value.X;
cursor.TranslationY = clickedPosition.Value.Y;
}
}
}