忍者ブログ
  • 2024.08
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 2024.10
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

【2024/09/17 09:49 】 |
スクリーン座標の変換
座標系を、別のビュー相対座標に変換する方法

CGRect convertedView = [view convertRect:view.bounds toView:otherView];

これでRectをotherView相対に変換できる。
boundsとframeの違いに注意。もちろんどちらもCGRectなので使えるが、間違えないよう注意。
(boundsはそのビューの座標系。frameはそのビューの親ビューの座標系。
よってboundsのoriginは常に(0,0)になっている)
普通はboundsを使うことになるだろう。

ちなみに、toView を nil にすると 画面相対の座標が手に入る。便利!

ちなみに、CGRectでなくCGPointもある。

UIViewのメソッド
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;



ビュー相対でなく、ウィンドウ相対で変換できる同様のメソッドもある。

UIWindowのメソッド
- (CGPoint)convertPoint:(CGPoint)point toWindow:(UIWindow *)window;
- (CGPoint)convertPoint:(CGPoint)point fromWindow:(UIWindow *)window;
- (CGRect)convertRect:(CGRect)rect toWindow:(UIWindow *)window;
- (CGRect)convertRect:(CGRect)rect fromWindow:(UIWindow *)window;

メインウィンドウを取得するには

UIWindow *window = ((AppDelegate*)[UIApplication sharedApplication].delegate).window;


とする。(大抵の場合、AppDelegateにwindowインスタンスがあるので)

ただし、一度これで変換してみたところ全然違う座標に変換されて返ってきた。原因は深く追ってないが。
スクリーン座標が欲しいときは、
convertRect:toView: で引数を nil にするのが早い。


PR
【2014/11/02 15:34 】 | iPhone | 有り難いご意見(0)
<<Xcodeでプロジェクト名その他を変更する | ホーム | 同じ証明書で別のPCでprovisioningを使いたいとき>>
有り難いご意見
貴重なご意見の投稿














<<前ページ | ホーム | 次ページ>>