用過IPhone的都知道,IPhone相冊裏,當圖片放大到必定程度後,手指一放,會自動縮回,移動圖片超出邊框後手指一放,圖片也會自動縮回,整個過程很是和諧、天然、精確,那麼WPF可否作到呢,答案是確定的。ide
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); }
這樣一來,仿IPhone的反彈效果就已經差很少了。其實這裏是利用了UI的MatrixTransform作了動畫,有人說不是有個MatrixAnimationUsingKeyFrames動畫類嗎,有興趣的人能夠繼續探索下。動畫
鑑於你們的熱情,我會把所有代碼整理出來放到羣文件共享裏,敬請關注。spa