0

I'm working on an iPad app. I have a view in which the user can draw with his finger. This view has a subview, which is a calculator, and which have buttons.

I would like that when the user touch a button, the superview (in which the user can draw) doesn't take into account this touch. (so the user doesn't draw when he touches the calculator)

Preferably, I would like to not change the code of the calculator view and the code of my superview. I have only access to them via properties of another class.

Is there a way to solve the problem please? I have tried exclusiveTouch, but it doesn't work.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
Arnaud
  • 4,884
  • 17
  • 54
  • 85

2 Answers2

2

If you have access to the button action and the drawing gesture you can simply set:

gesture.enabled = NO;

To cancel the current gesture processing and / or prevent it from starting. When you want to reenable the gesture depends on what type it is and how it's used but doing it immediately (on the next line) will probably work ok.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Unfortunately, the button action is in the Calculator class, which is used by a lot of other classes. I prefer to not modify it. – Arnaud Aug 26 '13 at 09:03
  • You will need to change something. Do you have access to the gesture / touch handlers? – Wain Aug 26 '13 at 09:14
1

Try this but include UIGestureRecognizerDelegate in your header file.

This is from apple "SimpleGestureRecognizers" example-

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{
    // Disallow recognition of tap gestures in the segmented control.
    if ((touch.view == YourButton)) 
    {
        //change it to your condition
        return NO;
    }

    return YES;
}
anky_believeMe
  • 484
  • 1
  • 4
  • 14
  • I tried this, but the method is never called. Is it really in my class that I should put this, and not in the calculator class ? – Arnaud Aug 26 '13 at 10:15
  • Did you include this `UIGestureRecognizerDelegate` and also try adding `- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; }` – anky_believeMe Aug 26 '13 at 10:22
  • Now yes, but it doesn't work. But I have solved my problem in another way (outside the scope of my question). Thank you for your help. – Arnaud Aug 26 '13 at 11:29
  • You should either indicate that this code was drawn from Apple's SimpleGestureRecognizers example, or from this answer: http://stackoverflow.com/questions/4825199/gesture-recognizer-and-button-actions/4825261#4825261 . – Brad Larson Aug 26 '13 at 17:48
  • This was a code I had used in my project. I have made the necessary edits though. – anky_believeMe Aug 26 '13 at 18:15
  • 1
    Note that the problem with this answer is that you probably didn't set your gesture recognizer's `delegate`. The method won't get called if it's not the delegate of any gestures. – rebello95 Mar 27 '15 at 00:41