PanGestureRecognizer 在拖动过程中零星出现

Posted

技术标签:

【中文标题】PanGestureRecognizer 在拖动过程中零星出现【英文标题】:PanGestureRecognizer sporadic during drag 【发布时间】:2020-06-18 10:48:26 【问题描述】:

我正在使用 PanGestureRecognizer 为图像创建一个简单的拖动动画。但是,当拖动正在进行时,TotalX 和 TotalY 值不一致,当我拖动时,它们似乎在 2 个值之间跳跃/闪烁/颤动。以下是值的原始跟踪示例:

[0:] PanUpdated: StatusType: Started, GestureId: 0, TotalX: 0, TotalY: 0
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 11.1617584228516, TotalY: 11.9234619140625
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 2.45475260416667, TotalY: 2.80341593424479
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 11.8422292073568, TotalY: 12.5428161621094
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 3.20952860514323, TotalY: 3.77924601236979
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 12.6615346272786, TotalY: 13.5227355957031
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 4.03237915039063, TotalY: 4.76089477539063
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 12.8976643880208, TotalY: 14.1595052083333
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 4.93493143717448, TotalY: 5.72846476236979
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 13.551747639974, TotalY: 14.7833862304688
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 5.67492167154948, TotalY: 6.68665568033854
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 14.1307627360026, TotalY: 15.5877075195313
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 6.67552185058594, TotalY: 7.82212320963542
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 14.8915608723958, TotalY: 16.3966369628906
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 7.67821248372396, TotalY: 8.44764200846354
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 15.2916819254557, TotalY: 16.9883219401042
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 8.57093811035156, TotalY: 9.37423706054688
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 15.8519948323568, TotalY: 17.2493794759115
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 9.13496907552083, TotalY: 10.1264038085938
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 16.5894927978516, TotalY: 17.6655578613281
[0:] PanUpdated: StatusType: Running, GestureId: 0, TotalX: 10.049072265625, TotalY: 11.0558268229167
[0:] PanUpdated: StatusType: Completed, GestureId: 0, TotalX: 0, TotalY: 0

但是,如果我将手势添加到容器中,则值会正常流动。

这是我的页面:

<ContentPage>
    <AbsoluteLayout >
        <Image x:Name="Card" Source="CardBack_Red.png" />
    </AbsoluteLayout>
</ContentPage>

以及背后的代码:

    public partial class MainPage : ContentPage
    
        double x, y;

        public MainPage()
        
            InitializeComponent();

            var panGesture = new PanGestureRecognizer();
            panGesture.PanUpdated += PanGesture_PanUpdated;
            panGesture.TouchPoints = 1;
            Card.GestureRecognizers.Add(panGesture);
        

        private void PanGesture_PanUpdated(object sender, PanUpdatedEventArgs e)
        
            Debug.WriteLine($"PanUpdated: StatusType: e.StatusType, GestureId: e.GestureId, TotalX: e.TotalX, TotalY: e.TotalY");

            Image card = (Image)sender;

            switch (e.StatusType)
            
                case GestureStatus.Started:
                    x = card.TranslationX;
                    y = card.TranslationY;
                    break;
                case GestureStatus.Running:
                    card.TranslationX = x + e.TotalX;
                    card.TranslationY = y + e.TotalY;
                    break;
            
        
    

好像我在打什么,但我不知道是什么,有什么想法吗?

谢谢

-约翰

【问题讨论】:

现在可以用了吗? 【参考方案1】:

已解决

原来我只是使用了错误的平移算法,GestureStatus.Running 案例需要将增量直接添加到图像平移坐标中,而不是添加起始平移。我上面的代码中的情况如下所示:

        case GestureStatus.Running:
            dragCard.TranslationX += e.TotalX;
            dragCard.TranslationY += e.TotalY;
            break;

我找到了更好的 sample here,帮了很多忙。

【讨论】:

以上是关于PanGestureRecognizer 在拖动过程中零星出现的主要内容,如果未能解决你的问题,请参考以下文章

Swift - 带有英雄动画的 PanGestureRecognizer

如何允许在某个区域拖动 UIView (PanGesture ..)

UIPanGestureRecognizer 拖动TableView改变其高度

在 iOS 中,为啥拖过 UIImageView 不会结束 PanGestureRecognizer?

PanGestureRecognizer 的有限区域

使用 pangesturerecognizer 创建边界以阻止对象进入状态栏下方?