Skip to content

Latest commit

 

History

History
82 lines (57 loc) · 7.2 KB

rotatetransform.md

File metadata and controls

82 lines (57 loc) · 7.2 KB
-api-id -api-type
T:Microsoft.UI.Xaml.Media.RotateTransform
winrt class

Microsoft.UI.Xaml.Media.RotateTransform

-description

Rotates an object around a specified point in a two-dimensional x-y coordinate system.

-xaml-syntax

<RotateTransform .../>

-remarks

A RotateTransform is defined by an Angle that rotates an object through an arc around the point CenterX, CenterY.

If the Angle value applied is positive, the rotation applied is in the clockwise direction. It's legal to use an Angle value that's negative, which causes the rotation to be counterclockwise. For values less than –360 or greater than 360, the values wrap around and are treated as if the mathematical operation mod(360) was applied.

To rotate in place, leave CenterX, CenterY as the default (0,0). You might use a nondefault CenterX, CenterY if you don't want to rotate in place and instead want to rotate around a point in the transform's frame of reference. For example, you can simulate an orbit.

A Transform is typically used to fill the UIElement.RenderTransform property, to change how an element renders. UIElement also has the UIElement.RenderTransformOrigin property, which defaults to (0,0). RenderTransformOrigin establishes the coordinate frame of reference for how all transformations including the RotateTransform will apply. A common scenario for RotateTransform is to rotate an object in place around its center (either as an animation or as a one-time transformation). With the default UIElement.RenderTransformOrigin of (0,0) an object won't rotate around its center, it rotates around the top left corner of its bounding box. Therefore, the common way to cause an object to rotate around its center is to leave CenterX, CenterY as (0,0) but set UIElement.RenderTransformOrigin to be a logical Point where the values are (0.5,0.5). Using the logical point convention, that puts the UIElement.RenderTransformOrigin at the center point of the object, in other words at an origin where (x,y) are exactly half of the ActualHeight and ActualWidth values.

UIElement.RenderTransformOrigin uses the logical point convention; CenterX and CenterY don't use that convention, they use actual pixel values.

The rendering position for an object can be offset on a Canvas using Canvas.Left and Canvas.Top, but this does not count as a transformation; the object retains its own local (0,0) origin when it's positioned in a Canvas.

There are other properties and relationships that can affect how the rotation appears. If there are multiple transformations applied by using a TransformGroup, the order matters. The transformations are applied in the order that they appear in the TransformCollection. Especially if one of the transformations is a TranslateTransform, you might have to alter the order to get the rotation effect you want.

There are three ways to apply multiple transformations to the same object:

  • Using a TransformGroup, where you can specify the order that each transformation applies.
  • Using a CompositeTransform, where each of the transformations is enabled by properties of a shared Transform object and the transformations are applied in a fixed, known order.
  • Using a MatrixTransform, where you set the various properties that control the 3×3 matrix in such a way that you're combining the typical classifications of transformations into one Transform. Unless you're using a design tool to help set the values, this is probably the most advanced technique.

Animating a RotateTransform

You can apply an animation to a RotateTransform to cause an element to rotate over time. Typically you only apply the animation to the Angle property, and don't animate CenterX, CenterY. For a continuously spinning animation, you'd typically use just the To value for a From/To/By style animation. Angle is a Double so this involves a DoubleAnimation. For a continuous animation you'd set the RepeatBehavior of the DoubleAnimation to Forever.

<Page.Resources>
  <Storyboard x:Name="spinrect">
     <DoubleAnimation To="360" RepeatBehavior="Forever" 
         Storyboard.TargetName="spinme"
         Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" />
  </Storyboard>
</Page.Resources>
<StackPanel>
  <Rectangle Name="spinme" Width="50" Height="50" Fill="Red" RenderTransformOrigin=".5,.5"
      PointerPressed="spinme_PointerPressed">
    <Rectangle.RenderTransform>
      <RotateTransform/>
    </Rectangle.RenderTransform>
  </Rectangle>
</StackPanel>
private void spinme_PointerPressed(object sender, PointerRoutedEventArgs e)
{
    spinrect.Begin();
}

-examples

Transforms can alter the display of text in your application to create a decorative effect. This example shows text rotated 90 degrees using a RotateTransform.

This example uses a RotateTransform to rotate text. An Angle value of 90 rotates the element 90 degrees clockwise.

[!code-xamlRotateTransform90DegreeText]

-see-also

XAML two-dimensional transforms sample, Transform