iPadアプリを作成しています。UISearchBarを、ユーザーがUISearchBarの外側をタップした場合にそれ自体を閉じるビューとして実装しようとしています。
[検索]ボタンをタップすると、検索バーが作成され、テーブルビューの上の位置にアニメーション化されます。また、UITapGestureRecognizerサブクラスを作成し(これについては後で説明します)、アプリのウィンドウに追加します。
- (void) searchTap:(id)sender
{
if (!self.controller.filterBar)
{
_filterBarStartFrame = CGRectMake(0.0, 44.0, 320.0, 0.0);
CGRect filterBarEndFrame = CGRectMake(0.0, 0.0, 320.0, 44.0);
_tableViewStartFrame = self.controller.tableView.frame;
CGRect tableViewEndFrame = CGRectMake(self.controller.tableView.frame.origin.x, self.controller.tableView.frame.origin.y + 44.0, self.controller.tableView.frame.size.width, self.controller.tableView.frame.size.height - 44.0);
self.controller.filterBar = [[UISearchBar alloc] initWithFrame:_filterBarStartFrame];
self.controller.filterBar.delegate = self;
[self.controller.tableView.superview addSubview:self.controller.filterBar];
[UIView animateWithDuration:0.5 animations:^{self.controller.tableView.frame = tableViewEndFrame;}];
[UIView animateWithDuration:0.5 animations:^{self.controller.filterBar.frame = filterBarEndFrame;}];
tgr = [[FFTapGestureRecognizer alloc] initWithTarget:self action:@selector(filterBarTap:)];
[[[UIApplication sharedApplication] keyWindow] addGestureRecognizer:tgr];
[self.controller.filterBar becomeFirstResponder];
}
}
検索バーが表示されたら、すべてのシングルタップをキャプチャしてヒットテストします。タップが検索バーの外側にある場合は、検索バーを閉じます。
- (void) filterBarTap:(FFTapGestureRecognizer*) sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
if (![self.controller.filterBar hitTest:[sender locationInView:self.controller.filterBar] withEvent:nil])
{
//if tap is outside filter bar, close the filter bar
[self searchBarCancelButtonClicked:self.controller.filterBar];
}
else
{
//pass the tap up the responder chain
//THIS DOESN'T WORK!
[self.controller.filterBar touchesEnded:sender.touches withEvent:sender.event];
}
}
}
ただし、タップが検索バーの内側にある場合は、検索バーでタップを正常に処理する必要があります。これを行うために私が見ることができる唯一の方法は、touchesEndedを検索バーに送信し、touchsとイベントを渡すことでした。どちらも持っていないので、UITapGestureRecognizerをサブクラス化して、touchesEndedを受け取ったときに両方をキャプチャしました。
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event
{
self.touches = touches;
self.event = event;
[super touchesEnded:touches withEvent:event];
}
残りの3つのタッチ…メソッドも同様に再実装し、resetも再実装してそのスーパークラスを呼び出しました。
残念ながら、このトリックはすべて、フレーム内で発生するタップを検索バーに渡すことを除いて機能します。[キャンセル]ボタンをタップしても何も起こりません。[クリア]ボタンをタップしても何も起こりません。
誰かが私がこれを行う方法を教えてもらえますか?
ありがとう