Reputation: 408
I am writing a board game app (like chess). The main view recognizes swipe gestures (UISwipeGestureRecognizer) started anywhere in its fullscreen view, which make the board rotating.
Now I added a square-shaped transparent subview exactly over the board. This is an UIControl subclass that detects touches - as moves of pawns around the board:
[self.view addSubview:self.boardControl]
I expected that swipe gestures will be blocked at the area of screen that is covered with UIControl subclass. And they are not. So when I make a quick touch and drag (a swipe) over my square boardControl, it first is detected as a pawn move, but then is detected again as swipe which rotates the board.
How can I make my UIControl subclass to block a flow of touch events started in its frame to its superviews?
I can prevent my app from reacting to swipe gestures, by filtering touches at the level of superview, either by:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
CGPoint location = [gestureRecognizer locationInView:self.view];
CGRect frame = self.boardControl.frame;
if ( CGRectContainsPoint(frame, location) )
return NO;
return YES;
}
or by:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch
{
CGPoint location = [touch locationInView:self.view];
CGRect frame = self.boardControl.frame;
if (CGRectContainsPoint(frame, location))
return NO;
return YES;
}
but I want to solve that issue one level earlier: I want boardControl to not pass up any of started within their frame touch events, higher in views hierarchy.
Can UIControl subclass "cover" its superview, and "eat" all touches it gets, so the superview will not need to access its frame to guess if such a touch has to be filtered out or not?
Upvotes: 0
Views: 645
Reputation: 5684
All you need is implement UIGestureRecognizerDelegate
is provides way to make requested things.
I think you should start from gestureRecognizer:shouldReceiveTouch:
examples
Upvotes: 0