0

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?

Voyteck
  • 354
  • 2
  • 13

1 Answers1

0

All you need is implement UIGestureRecognizerDelegate is provides way to make requested things.

I think you should start from gestureRecognizer:shouldReceiveTouch: examples

sage444
  • 5,661
  • 4
  • 33
  • 60
  • Do you mean something like this:
    ` - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { CGPoint location = [gestureRecognizer locationInView:self.view]; CGRect frame = self.boardControl.frame; if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] && CGRectContainsPoint(frame, location)) return NO; }`
    This works, however I want my boardControl to no **pass through** any touches/gestures that started in its view. Isn't that possible?
    – Voyteck Feb 28 '14 at 15:50
  • This code works, however I want my boardControl to **not pass through** any touches/gestures that started in its view. Isn't that possible? *(Sorry, I didn't manage to format my previous comment as a code).* – Voyteck Feb 28 '14 at 16:04
  • add code to question, and yes, this code looks right, are you tried also implement `gestureRecognizer:shouldReceiveTouch ` if yes, please also update question – sage444 Feb 28 '14 at 16:13