问题
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