kade

技術系の記事を書いていきます。

Objective-C: タッチ系イベントを取得する

Swiftを使おうと思いつつもiOS8でアプリリリースをするとなると結構先になってしまうので、まだまだObjective-Cで書いてた方がいい感じです。

タッチ系Eventの取得は以下になります。スワイプとかただのドラッグとかはもっといい関数がありますが、これだと汎用性高く色々やれると思います。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  CGPoint pt = [[touches anyObject] locationInView:self.view.window];
  NSLog(@"touchesBegan:%.0f, %.0f", pt.x, pt.y);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  CGPoint pt = [[touches anyObject] locationInView:self.view.window];
  NSLog(@"touchesMoved:%.0f, %.0f", pt.x, pt.y);
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  CGPoint pt = [[touches anyObject] locationInView:self.view.window];
  NSLog(@"touchesEnded:%.0f, %.0f", pt.x, pt.y);
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
  CGPoint pt = [[touches anyObject] locationInView:self.view.window];
  NSLog(@"touchesCancelled:%.0f, %.0f", pt.x, pt.y);
}