In iOS is it possible to change a View's coordinate system so that 0,0 is top right corner?

1k views Asked by At

Normally the 0,0 coordinate refers to the top left corner of a view. Higher x coordinates are further right. A frame / rectangle in the view has its leftmost point being its x coordinate and its rightmost point being its x coordinate plus its width.

Is it possible to reverse that, or better yet, reverse just the x axis? Make the 0,0 be the top right. Make the higher origins be further to the left. AND make it so a frame / rectangle in the view has its rightmost point as its x coordinate and its leftmost point as its x coordinate plus its width.

I know I could transform this stuff myself with pure math, but I was wondering if iOS offers this capability.

2

There are 2 answers

0
Aaron Brager On

Not really.

  • iOS 9 has some new flipping stuff for supporting right-to-left languages, but I don't think you can force it.
  • You can flip the drawing of a view by setting its transform property to CGAffineTransformMakeScale(-1, 1), but that won't change the underlying coordinate system.
  • SpriteKit has a different coordinate system than normal UIViews, but its coordinate system isn't what you want.

You may want to read Coordinate Systems and Transforms, which discusses some techniques for mapping points between different coordinate systems. This MSDN article covers mapping points using matrices, which can help you on a theoretical level.

0
Aaron Ash On

It's not documented, but UIView has an instance variable, _flipsHorizontalAxis, that does exactly what it sounds like it would do. It looks like it just passes through the CALayer variable of the same name.