How to Animate a Visual.TransformMatrix?

痞子三分冷 提交于 2019-12-11 05:15:48

问题


TransformMatrix documentation says "This property can be animated. Call CompositionObject.StartAnimation to associate it with a CompositionAnimation."

But I can't create a CompositionAnimation that can target it. I tried CreateQuaternionKeyframeAnimation and I tried CreateVector4KeyframeAnimation:

Visual visual = ElementCompositionPreview.GetElementVisual(myUIElement);
var animation = visual.Compositor.CreateVector4KeyFrameAnimation();
Vector4 v4 = Vector4.Transform(new Vector4(), myMatrix4x4);
animation.InsertKeyFrame(1f, v4);
animation.Duration = TimeSpan.FromMilliseconds(3000);
visual.StartAnimation(nameof(visual.TransformMatrix), animation);

Which throws "The expression output does not match animating property type" as one might expect. I don't know what else to try. Do I have to create 16 separate scalar animations, one for each matrix value? That seems a bit crazy.

EDIT: So I tried to just roll my own and use 16 scalar animations like so:

for (int i = 5; i <= 20; i++)
{
    int row = (i / 4);
    int column = (i % 4);
    if (column == 0) { column = 4; }
    var matrixAnimation = visual.Compositor.CreateScalarKeyFrameAnimation();
    var matrixEasing = visual.Compositor.CreateLinearEasingFunction();
    matrixAnimation.InsertExpressionKeyFrame(1f, "this.FinalValue", matrixEasing);
    matrixAnimation.Target = nameof(visual.TransformMatrix) + ".M" + row + column;
    matrixAnimation.Duration = TimeSpan.FromMilliseconds(durationMilliseconds);
    visual.ImplicitAnimations[nameof(visual.TransformMatrix) + ".M" + row + column] = matrixAnimation;
}

So the implicit animations are created and all seems great. Then to my dismay I write the code to change the read/write M11- M44 properties which ought to trigger the implicit animation and I get a compiler error:

"Cannot modify the return value of 'Visual.TransformMatrix' because it is not a variable".

And assigning it a new Matrix4x4 does not trigger the implicit animation.

来源:https://stackoverflow.com/questions/53273936/how-to-animate-a-visual-transformmatrix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!