跳到主要内容
新架构实战课 实操 + 基建 + 原理全维度包揽,抢先掌握 React Native 新架构精髓 立即查看 >Version: 0.71

Transforms

Transforms are style properties that will help you modify the appearance and position of your components using 2D or 3D transformations. However, once you apply transforms, the layouts remain the same around the transformed component hence it might overlap with the nearby components. You can apply margin to the transformed component, the nearby components or padding to the container to prevent such overlaps.

Example


Reference

Transform

transform accepts an array of transformation objects or space-separated string values. Each object specifies the property that will be transformed as the key, and the value to use in the transformation. Objects should not be combined. Use a single key/value pair per object.

The rotate transformations require a string so that the transform may be expressed in degrees (deg) or radians (rad). For example:

{
transform: [{rotateX: '45deg'}, {rotateZ: '0.785398rad'}],
}

The same could also be achieved using a space-separated string:

{
transform: 'rotateX(45deg) rotateZ(0.785398rad)',
}

The skew transformations require a string so that the transform may be expressed in degrees (deg). For example:

{
transform: [{skewX: '45deg'}],
}
TypeRequired
array of objects: {matrix: number[]}, {perspective: number}, {rotate: string}, {rotateX: string}, {rotateY: string}, {rotateZ: string}, {scale: number}, {scaleX: number}, {scaleY: number}, {translateX: number}, {translateY: number}, {skewX: string}, {skewY: string} or stringNo

decomposedMatrix, rotation, scaleX, scaleY, transformMatrix, translateX, translateY

Deprecated. Use the transform prop instead.

Transform Origin

The transformOrigin property sets the origin for a view's transformations. The transform origin is the point around which a transformation is applied. By default, the origin of a transform is center.

Example

Values

Transform origin supports px, percentage and keywords top, left, right, bottom, center values.

The transformOrigin property may be specified using one, two, or three values, where each value represents an offset.

One-value syntax:

  • The value must be a px, a percentage, or one of the keywords left, center, right, top, and bottom.
{
transformOrigin: '20px',
transformOrigin: 'bottom',
}

Two-value syntax:

  • First value (x-offset) must be a px, a percentage, or one of the keywords left, center, and right.
  • The second value (y-offset) must be a px, a percentage, or one of the keywords top, center, and bottom.
{
transformOrigin: '10px 2px',
transformOrigin: 'left top',
transformOrigin: 'top right',
}

Three-value syntax:

  • The first two values are the same as for the two-value syntax.
  • The third value (z-offset) must be a px. It always represents the Z offset.
{
transformOrigin: '2px 30% 10px',
transformOrigin: 'right bottom 20px',
}

Array syntax

transformOrigin also supports an array syntax. It makes it convenient to use it with Animated APIs. It also avoids string parsing, so should be more efficient.

{
// Using numeric values
transformOrigin: [10, 30, 40],
// Mixing numeric and percentage values
transformOrigin: [10, '20%', 0],
}

You may refer to MDN's guide on Transform origin for additional information.