ios - 在 iOS 中禁用父自定义控件中的手势

标签 ios xamarin.forms gesture-recognition skiasharp

我有一个自定义控件“容器”来管理手势,里面有一个 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/

相关文章:

ios - UIImagePicker 图像大小大于原始大小

webview - 如何在加载数据时获取WebView的进度,Xamarin.Forms

ios - UILongPressGestureRecognizer- 如何在按住的同时增加大小

android - 在不消耗触摸事件的情况下实现 listItem onClickListener

c# - 如何在自定义 Xamarin.Forms ViewCell 的行之间添加分隔符?

Android - 特定 View 上的手势检测(向上/向下滑动)

ios - 如何以编程方式切换 UISegmentedControl?

ios - 如何将音频单元输出的相位移动 180 度

ios - 我的应用程序在 iOS 模拟器中不显示任何内容

c# - 如何在 Xamarin 中为 DatePicker 添加最小日期验证检查