- iOS 10.0+
- macOS 10.12+
- tvOS 10.0+
- watchOS 3.0+
You create an
SKWarp by supplying two arrays of normalized vertex positions: the
source array defines the positions of the vertices in the un-warped geometry and the
dest defines the final, warped destination positions for the source vertices. Both arrays are one dimensional and in “row-major” order — this means that you additionally have to supply the number of columns and rows of the geometry.
The number of columns or rows is one less than the number of horizontal or vertical vertices respectively.
The origin of the vertex positions is in the bottom left: The following code shows how you can create an array of evenly spaced vertices which could act as a default set of source positions for a warp geometry with two columns and two rows. The first item in the array refers to the position at the bottom left and the last item refers to the position at the top right.
If you wanted to give the effect of horizontally squeezing a grid defined by the above vertices around its middle — which would cause it to stretch vertically — you could use a set of destination positions such as:
You use these two arrays to define a warp geometry object.
A geometry grid warps the geometry defined by the source positions (left illustration) into a new geometry defined by the destination positions (right illustration).
The geometry can be applied immediately, without animation, by setting the nodes’s
warp property. You may elect to use this technique if you are calculating and applying the destination positions with each scene update, for example in response to a user’s touch.
A single warp can be applied over time to give an animated morphing effect using the action created by
warp(to:. To use a warp action, you first need to define an initial geometry and set it as the
warp of the node you want to warp. The
init(columns: initializer creates a suitable geometry that has no distortion.
In the following code listing, the stretched warp geometry created above is used to initialize a warp action with a duration specified in seconds. The node’s run method executes the action.
You can chain together multiple warp geometries to create complex morphing animations. For example, you may want to begin with the warp geometry that does not deform the node, morph to the stretched geometry at 0.5”, and finish the sequence by returning to the first geometry at 0.75”. The following code creates this action, using the
Objects that subclass
SKNode but don't conform to
SKWarpable — for example
SKVideo — can be warped by adding them as children of an
SKEffect object. You can, for example, use this approach to distort vector artwork supplied as simple geometric primitives or
CGPath objects and rendered with an shape node. Warping an particle emitter node that has been added as a child of an effect node allows precise control of the overall shape of a particle system.