Edytuj

Udostępnij za pośrednictwem


CGContext.GetCTM Method

Definition

Retrieves the current Context Transformation Matrix.

public CoreGraphics.CGAffineTransform GetCTM ();
member this.GetCTM : unit -> CoreGraphics.CGAffineTransform

Returns

The CGAffineTransform currently being used by the CGContext.

Remarks

The CGContext has, as part of its drawing state, a CGAffineTransform called the Context Transformation Matrix (CTM).

By default, the CTM's X and Y axes increase to the right and downward, as indicated by the red rays in the following image.

A common transform is to locate the origin in the lower left-hand corner, with X and Y increasing to the right and upward, as shown with the green rays in the following image (note that, for visibility, the rays are set to originate at (5,5) rather than (0,0)). This is done with ScaleCTM(1,-1) and TranslateCTM(0, -Bounds.Height) (see example below).

More complex transforms are, possible, as illustrated by the blue rays, which illustrate a transform that is both translated and rotated. Note that manipulation of the CTM is stateful and order-dependent: the final transform is translated and rotated relative to the CTM used to the draw the 2nd-to-last green rays.

The following example shows the manipulation of the CTM to create the example image.

public override void Draw (RectangleF rect)
{
	base.Draw (rect);

	using (var ctxt = UIGraphics.GetCurrentContext ()) {
		DrawRay (ctxt, new PointF (5, 5), new PointF (105, 5), UIColor.Red.CGColor);
		DrawRay (ctxt, new PointF (5, 5), new PointF (5, 105), UIColor.Red.CGColor);
		ctxt.ScaleCTM (1, -1);
		ctxt.TranslateCTM (0, -Bounds.Height);
		DrawRay (ctxt, new PointF (5, 5), new PointF (105, 5), UIColor.Green.CGColor);
		DrawRay (ctxt, new PointF (5, 5), new PointF (5, 105), UIColor.Green.CGColor);

		var cos = .707f;
		var sin = -.707f;
		var xform = new CGAffineTransform (cos, sin, -sin, cos, Bounds.Width / 2, 100);

		ctxt.ConcatCTM (xform);
		DrawRay (ctxt, new PointF (0, 0), new PointF (100, 0), UIColor.Blue.CGColor);
		DrawRay (ctxt, new PointF (0, 0), new PointF (0, 100), UIColor.Blue.CGColor);

	}
}              

Applies to

See also

  • <xref:CoreGraphics.CGContext.ConcatCTM>
  • <xref:CoreGraphics.CGContext.RotateCTM>
  • <xref:CoreGraphics.CGContext.ScaleCTM>
  • <xref:CoreGraphics.CGContext.TranslateCTM>