タッチを監視してから消えるオーバーレイビューを作成しようとしていますが、ビューの下にあるものにタッチイベントを転送します。
私のテストアプリには、ボタンが入ったビューがあります。オーバーレイビューを別のサブビュー(基本的にはボタンの兄弟)として追加します。これは画面全体を占めます。
私が試した両方のソリューションで、オーバーレイは状態を保持して、タッチにどのように反応するかを決定します。touchesBeganイベントを受信すると、touchesCancelledまたはtouchsEndedを受信するまで、オーバーレイはhitTestまたはpointInsideへの応答を停止します。
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if(_respondToTouch)
{
NSLog(@"Responding to hit test");
return [super hitTest:point withEvent:event];
}
NSLog(@"Ignoring hit test");
return nil;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
if(_respondToTouch)
{
NSLog(@"Responding to point inside");
return [super pointInside:point withEvent:event];
}
NSLog(@"Ignoring point inside");
return NO;
}
最初のアプローチとして、イベントを再発行してみました。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(!_respondToTouch)
{
NSLog(@"Ignoring touches began");
return;
}
NSLog(@"Responding to touches began");
_respondToTouch = NO;
[[UIApplication sharedApplication] sendEvent:event];
}
- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touches cancelled");
_respondToTouch = YES;
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touches ended");
_respondToTouch = YES;
}
ただし、ボタンは再発行されたイベントに応答しませんでした。
2番目のアプローチは、hitTestを使用してオーバーレイの下のビュー(マイボタン)を検出し、touchesXXXメッセージを直接送信することでした。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touches began");
_respondToTouch = NO;
UITouch* touch = [touches anyObject];
_touchDelegate = [[UIApplication sharedApplication].keyWindow hitTest:[touch locationInView:self.superview] withEvent:event];
CGPoint locationInView = [touch locationInView:_touchDelegate];
NSLog(@"Sending touch %@ to view %@. location in view = %f, %f", touch, _touchDelegate, locationInView.x, locationInView.y);
[_touchDelegate touchesBegan:touches withEvent:event];
}
- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touches cancelled");
[_touchDelegate touchesCancelled:touches withEvent:event];
_respondToTouch = YES;
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touches ended");
[_touchDelegate touchesEnded:touches withEvent:event];
_respondToTouch = YES;
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touches moved");
[_touchDelegate touchesMoved:touches withEvent:event];
}
ボタンは(ログによると)検出されますが、touchesXXXを呼び出してもボタンはまったく反応しません。
ボタンがtouchesBeganの直接呼び出しに応答しないため、他に何を試すべきかわかりません= /