LoginSignup
8
8

More than 5 years have passed since last update.

iOS8 ではデバイスに対して固定の座標値を得る方法が変わった

Posted at

UICoordinateSpace プロトコルの記述によれば、
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICoordinateSpace_protocol/index.html

Window と Screen の座標系原点は、iOS7 まではデバイスをポートレートにした時の左上に固定されていたが、iOS8 からはユーザーから見て常に左上になるように UI の回転に合わせて移動するようになったとのこと。(言い換えると、iOS7 までは Window と Screen はデバイスに固定で View がその中で回転していたが、iOS8からはScreen 自体がデバイスに対して回転するようになった。)

このため、iOS7までは

CGPoint in_window = [view convertPoint:point toView:nil];
CGPoint in_screen = [view.window convertPoint:in_window toWindow:nil];

とすると、UI の向きによらずデバイスに対して固定した座標値になりましたが、iOS8 からはこのコードでは向きに依存した値になってしまいます。

iOS8 以降では、新設された API を使って

CGPoint in_screen = [view convertPoint:input toCoordinateSpace:view.window.screen.fixedCoordinateSpace];

とするとのこと。API には次のようなバリエーションが有ります。

convertPoint:toCoordinateSpace:
convertPoint:fromCoordinateSpace:
convertRect:toCoordinateSpace:
convertRect:fromCoordinateSpace:

(View は UICoordinateSpace プロトコルを実装しているので、View はこのメッセージのレシーバーにも引数にも対照的に使えるのが少しトリッキー。でも美しい。)

参考:
http://stackoverflow.com/questions/24150359/is-uiscreen-mainscreen-bounds-size-becoming-orientation-dependent-in-ios8
https://developer.apple.com/videos/wwdc/2014/#214

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8