sách gpt4 ai đã đi

ListView 滑动/滑动动画

In lại 作者:行者123 更新时间:2023-12-02 15:26:31 26 4
mua khóa gpt4 Nike

我有一个 ListView 控件来显示项目,我想提供一个滑动/滑动手势来选择一个项目。我用 GestureRecognizer类来识别交叉滑动手势,但我还想通过水平移动选择的项目来为这个手势设置动画。

例如,在 iOS 应用程序的这张图片上应该是这样的:

nhập mô tả hình ảnh ở đây

我在网络上进行了搜索,但找不到任何有用的链接来说明如何在 ListView 控件中为该手势设置动画。

1 Câu trả lời

您可以创建一个行为来监听项目上的 ManipulationXYZ 事件,然后为这些项目上的 RenderTransform 设置动画。我给你写了一个简单的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Microsoft.Xaml.Interactivity;

namespace SOTestApp
{
[TypeConstraint(typeof(FrameworkElement))]
public class SlideMechanicBehavior : DependencyObject, IBehavior
{
public void Attach(DependencyObject associatedObject)
{
AssociatedObject = associatedObject;
var fw = (FrameworkElement) AssociatedObject;
fw.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.System;
fw.ManipulationDelta += fw_ManipulationDelta;
fw.ManipulationCompleted += fw_ManipulationCompleted;
if (fw.RenderTransform == null || fw.RenderTransform as TranslateTransform == null)
{
fw.RenderTransform = new TranslateTransform();
}
}

private const double Threshold = 100.0;
private bool _canMove = true;

public ICommand LeftDragCommand
{
get { return (ICommand)GetValue(LeftDragCommandProperty); }
set { SetValue(LeftDragCommandProperty, value); }
}

public static readonly DependencyProperty LeftDragCommandProperty =
DependencyProperty.Register("LeftDragCommand", typeof(ICommand), typeof(SlideMechanicBehavior), new PropertyMetadata(default(ICommand)));

public ICommand RightDragCommand
{
get { return (ICommand)GetValue(RightDragCommandProperty); }
set { SetValue(RightDragCommandProperty, value); }
}

public static readonly DependencyProperty RightDragCommandProperty =
DependencyProperty.Register("RightDragCommand", typeof(ICommand), typeof(SlideMechanicBehavior), new PropertyMetadata(default(ICommand)));

void fw_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
var fw = (FrameworkElement)AssociatedObject;
var tr = (TranslateTransform) fw.RenderTransform;
if (tr == null) return;
tr.X = e.Cumulative.Translation.X;
//call commands
if (tr.X > Threshold && RightDragCommand != null && RightDragCommand.CanExecute(null)) RightDragCommand.Execute(null); //add params if necessary
if (tr.X < -1 * Threshold && LeftDragCommand != null && LeftDragCommand.CanExecute(null)) LeftDragCommand.Execute(null); //add params if necessary
//animate back
var s = new Storyboard();
var d = new DoubleAnimation
{
To = 0.0,
EasingFunction = new QuadraticEase(),
Duration = TimeSpan.FromMilliseconds(300.0)
};
Storyboard.SetTarget(d, tr);
Storyboard.SetTargetProperty(d, "X"); //use nameof() in C# 6.0
s.Children.Add(d);
_canMove = false;
s.Completed += (o, o1) => _canMove = true;
s.Begin();
}

void fw_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
if(!_canMove) return;
//move item
var m = e.Delta.Translation.X;
var fw = (FrameworkElement)AssociatedObject;
var tr = (TranslateTransform) fw.RenderTransform;
if (tr != null) tr.X += m;
}

public void Detach()
{
var fw = (FrameworkElement)AssociatedObject;
fw.ManipulationCompleted -= fw_ManipulationCompleted;
fw.ManipulationDelta -= fw_ManipulationDelta;
AssociatedObject = null;
}

public DependencyObject AssociatedObject { get; private set; }
}
}

您可以像这样在 Xaml 中使用它:













不要忘记在“项目”>“添加引用”>“扩展”对话框中添加 Behaviors SDK。

关于ListView 滑动/滑动动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30156189/

26 4 0
Bài viết được đề xuất: c - C 中的 LTRIM 函数
Bài viết được đề xuất: angularjs - 为什么我无法在 AngularJS 的 mdDialog Controller 中访问 `this` 的属性?
Bài viết được đề xuất: git - 保持 .git 尽可能小
Bài viết được đề xuất: Scala Futures - flatMap 和 onFailure
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com