WPF触控程序开发(三)——类似IPhone相册的反弹效果
2021-07-10 05:10
标签:也有 value pre getc 程序开发 文件共享 tor void entity 用过IPhone的都知道,IPhone相册里,当图片放大到一定程度后,手指一放,会自动缩回,移动图片超出边框后手指一放,图片也会自动缩回,整个过程非常和谐、自然、精确,那么WPF能否做到呢,答案是肯定的。 这样一来,仿IPhone的反弹效果就已经差不多了。其实这里是利用了UI的MatrixTransform做了动画,有人说不是有个MatrixAnimationUsingKeyFrames动画类吗,有兴趣的人可以继续探索下。 鉴于大家的热情,我会把全部代码整理出来放到群文件共享里,敬请关注。 WPF触控程序开发(三)——类似IPhone相册的反弹效果 标签:也有 value pre getc 程序开发 文件共享 tor void entity 原文地址:https://www.cnblogs.com/lonelyxmas/p/9688612.html public class LinearMatrixAnimation : AnimationTimeline
{
public Matrix? From
{
set { SetValue(FromProperty, value); }
get { return (Matrix)GetValue(FromProperty); }
}
public static DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(Matrix?), typeof(LinearMatrixAnimation), new PropertyMetadata(null));
public Matrix? To
{
set { SetValue(ToProperty, value); }
get { return (Matrix)GetValue(ToProperty); }
}
public static DependencyProperty ToProperty = DependencyProperty.Register("To", typeof(Matrix?), typeof(LinearMatrixAnimation), new PropertyMetadata(null));
public LinearMatrixAnimation()
{
}
public LinearMatrixAnimation(Matrix from, Matrix to, Duration duration)
{
Duration = duration;
From = from;
To = to;
}
public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock)
{
if (animationClock.CurrentProgress == null)
{
return null;
}
double progress = animationClock.CurrentProgress.Value;
Matrix from = From ?? (Matrix)defaultOriginValue;
if (To.HasValue)
{
Matrix to = To.Value;
Matrix newMatrix = new Matrix(((to.M11 - from.M11) * progress) + from.M11, 0, 0, ((to.M22 - from.M22) * progress) + from.M22,
((to.OffsetX - from.OffsetX) * progress) + from.OffsetX, ((to.OffsetY - from.OffsetY) * progress) + from.OffsetY);
return newMatrix;
}
return Matrix.Identity;
}
protected override System.Windows.Freezable CreateInstanceCore()
{
return new LinearMatrixAnimation();
}
public override System.Type TargetPropertyType
{
get { return typeof(Matrix); }
}
}
var animation = new LinearMatrixAnimation(oldMatrix, newMatrix, TimeSpan.FromSeconds(0.5));
animation.AccelerationRatio = 0.3;
animation.DecelerationRatio = 0.3;
matrixTransform.BeginAnimation(MatrixTransform.MatrixProperty, animation);
public static void PlayMatrixTransformAnimation(MatrixTransform matrixTransform, Matrix newMatrix, TimeSpan timeSpan)
{
var animation = new LinearMatrixAnimation(matrixTransform.Matrix, newMatrix, TimeSpan.FromSeconds(0.5));
animation.AccelerationRatio = 0.3;
animation.DecelerationRatio = 0.3;
animation.FillBehavior = FillBehavior.HoldEnd;
animation.Completed += (sender, e) =>
{
//去除属性的动画绑定
matrixTransform.BeginAnimation(MatrixTransform.MatrixProperty, null);
//将期望结果值保留
matrixTransform.Matrix = newMatrix;
};
//启动动画
matrixTransform.BeginAnimation(MatrixTransform.MatrixProperty, animation, HandoffBehavior.SnapshotAndReplace);
}
文章标题:WPF触控程序开发(三)——类似IPhone相册的反弹效果
文章链接:http://soscw.com/essay/103087.html