Skip to content

CGPoint ​

CGPoint represents a point on a plane: the x-coordinate x and the y-coordinate y, in pt. The origin is at the top left; x increases to the right and y increases downward.

Its fields are read-only; to change one, replace the whole struct with a new one. Where a struct is accepted on write, you can also give a plain object with all the fields. Each read of a property returns a new object. These rules hold for all structs; see Structs.

Inheritance: None. CGPoint is a plain JS class; it doesn't inherit from NSObject and isn't an EventTarget.

Constructor ​

CGPoint()

Creates a point from an x-coordinate and a y-coordinate. Omitted arguments are 0.

Instance properties ​

PropertyDescription
x read-onlyThe x-coordinate, in pt
y read-onlyThe y-coordinate, in pt

Examples ​

js
const view = new UIView();
view.frame = new CGRect(0, 0, 100, 100);
view.center = new CGPoint(200, 300);
console.log(String(view.center));

Effect: The view's center moves to (200, 300), and the console prints CGPoint(200, 300).

Corresponding iOS API ​

ObjCJS
CGPointMake(10, 20)new CGPoint(10, 20)
CGPointZeronew CGPoint()

See also ​

  • Structs: the rules shared by all structs, and converting to strings and JSON
  • Where it is used: center and convertPoint() on UIView, contentOffset on UIScrollView, and points on UIBezierPath
  • CGRect: origin is a CGPoint