我有一个自定义控件“容器”来管理手势,里面有一个 Skiasharp 可绘制控件。我正在寻找的功能是,在第一步中,用户可以缩放/移动容器内的图像,然后禁用容器的手势,用户可以用手指绘制可见图像。它曾经在几个月前工作,但当迁移到 xamarin 3.x 时,它开始失败。
这是容器的代码
public class GestureContainer : ContentView
{
private const double MIN_SCALE = 1;
private const double MAX_SCALE = 4;
private double startScale, currentScale;
private double startX, startY;
private double xOffset, yOffset;
private PanGestureRecognizer pan;
private PinchGestureRecognizer pinchGesture;
private TapGestureRecognizer tap;
public static readonly BindableProperty GestureOnProperty =
BindableProperty.Create("GestureOn", typeof(bool), typeof(GestureContainer), defaultValue: true, defaultBindingMode: BindingMode.TwoWay, propertyChanged: GestureOnChanges);
public bool GestureOn
{
get
{
return (bool)GetValue(GestureOnProperty);
}
set
{
SetValue(GestureOnProperty, value);
}
}
private static void GestureOnChanges(BindableObject bindable, object oldvalue, object newvalue)
{
GestureContainer gestureContainer = bindable as GestureContainer;
if ((bool)newvalue)
gestureContainer.SetGestures();
else
gestureContainer.GestureRecognizers.Clear();
}
public GestureContainer()
{
pinchGesture = new PinchGestureRecognizer();
pan = new PanGestureRecognizer();
tap = new TapGestureRecognizer { NumberOfTapsRequired = 2 };
SetGestures();
Scale = MIN_SCALE;
TranslationX = TranslationY = 0;
}
private void SetGestures()
{
pinchGesture.PinchUpdated += OnPinchUpdated;
GestureRecognizers.Add(pinchGesture);
pan.PanUpdated += OnPanUpdated;
GestureRecognizers.Add(pan);
tap.Tapped += OnTapped;
GestureRecognizers.Add(tap);
}
private void OnTapped(object sender, EventArgs e)
{
/**/
}
void RestoreScaleValues()
{
/**/
}
void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)
{
/**/
}
void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
/**/
}
}
具有“奇怪”功能的部分是这样的:
gestureContainer.GestureRecognizers.Clear();
当我将绑定(bind)属性启动为 false 时,会调用该方法,调用 .Clear(),但无论捏/平移/点击仍然有效,这会使绘图触摸无法正常工作
编辑
这是XAML代码
<controls:GestureContainer GestureOn="{Binding OptionsVisibility, Converter={StaticResource BooleanConverter}}" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All">
<controls:GestureContainer.Content>
<Grid>
<ffimageloading:CachedImage
Rotation="{Binding Rotation}"
x:Name="originalView"
Aspect="AspectFit"
DownsampleToViewSize="True"
Source="{Binding Imagen.ImageStream}"/>
<Grid IsEnabled="{Binding OptionsVisibility}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="Transparent">
<controls:BindableSKCanvasView x:Name="canvasView"
Color="{Binding StrokeColor}"
WidthStroke="{Binding StrokeWidth}"
EnableTouchEvents="True"
PaintSurface="OnCanvasViewPaintSurface" />
<Grid.Effects>
<skiacontrols:TouchEffect Capture="True" TouchAction="OnTouchEffectAction" />
</Grid.Effects>
</Grid>
</Grid>
</controls:GestureContainer.Content>
</controls:GestureContainer>
最佳答案
似乎正在创建多个引用。
尝试写一个这样的函数
void clearGesture() {
pinchGesture.PinchUpdated -= OnPinchUpdated;
pan.PanUpdated -= OnPanUpdated;
tap.Tapped -= OnTapped;
}
并明确地调用它,例如,gestureContainer.clearGesture();
关于ios - 在 iOS 中禁用父自定义控件中的手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54749383/