UIView にタッチイベントを追加するにはどうすればいいですか? 質問する

UIView にタッチイベントを追加するにはどうすればいいですか? 質問する

UIView にタッチ イベントを追加するにはどうすればよいですか? 次の
操作を試します:

UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, nextY)] autorelease];
[headerView addTarget:self action:@selector(myEvent:) forControlEvents:UIControlEventTouchDown];
// ERROR MESSAGE: UIView may not respond to '-addTarget:action:forControlEvents:'

サブクラスを作成して上書きしたくない

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

ベストアンサー1

iOS 3.2 以降では、ジェスチャ認識機能を使用できます。たとえば、タップ イベントを処理する方法は次のとおりです。

//The setup code (in viewDidLoad in your view controller)
UITapGestureRecognizer *singleFingerTap = 
  [[UITapGestureRecognizer alloc] initWithTarget:self 
                                          action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];

//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
  CGPoint location = [recognizer locationInView:[recognizer.view superview]];

  //Do stuff here...
}

組み込みのジェスチャーもたくさんあります。iOSのイベント処理とドキュメントをご覧くださいUIGestureRecognizer。サンプルコードもいくつかあります。ギットハブそれは役に立つかもしれません。

おすすめ記事