2012年12月23日日曜日

UIControlEventTouchUpOutsideで座標を取得


ボタンを押してドラッグし、ボタンの外で指を放した時の座標を取得する方法。

多くのアクションハンドラは以下の形式で足りるが
- (IBAction) touchUpOutside:(id)sender;

2番目の引数でイベント情報を受け取る形式を使用する。
- (IBAction) touchUpOutside:(id)sender forEvent:(UIEvent *)event;


eventの中身はこんな感じ
<UITouchesEvent: 0x1cd5a8d0> timestamp: 110165 touches: {(
    <UITouch: 0x1cd611c0>
      phase: Ended
      tap count: 0
      window: <UIWindow: 0x1cd90ca0; frame = (0 0; 320 480); opaque = NO; autoresize = RM+BM; layer = <UIWindowLayer: 0x1cd90da0>>
      view: <UIRoundedRectButton: 0x1cdbcf70; frame = (88 46; 44 64); alpha = 0.07; opaque = NO; autoresize = RM+H; tag = 64; layer = <CALayer: 0x1cdbd040>>
      location in window: {274, 458}
      previous location in window: {274, 459}
      location in view: {186, 42}
      previous location in view: {186, 43}
)}


座標の取得手順は、
NSSet *a = [event allTouches];
// (UITouch *touch in [a anyObject]) はストールするが、以下ならOK
CGPoint b = [[[a allObjects] objectAtIndex:0] locationInView:subview];
printf("(x,y)=(%f,%f)",b.x,b.y);

0 件のコメント:

コメントを投稿