Antoine Pastor
Antoine Pastor

Reputation: 207

Pass UIGestureRecognizer to the UIView below

I'm currently implementing a puzzle game where the player has to drag and drop images on a frame. I've attached a UIPanGestureRecognizer to each of the pieces of the puzzle and it works well. However, I've got a problem with the "L"-shaped pieces. The empty spaces of that piece are "blocking" the gestures recognition, and prevent the pieces at the bottom to receive the event.

for example, with "x" being my "L"-shaped piece, I cannot touch "a" :

xxx
xa

I've tried several things, like defining the shouldReceiveTouch delegate, so that it returns no when an empty space of the "L" is touched, but it doesn't pass the touch to the view below.

I've also defined panRecognizer.cancelsTouchesInView = NO; but this doesn't work either.

I was thinking of using nextresponder, but it seems that it is not possible with UIGestures.

If anyone has another idea, that would really help. Thanks.

Upvotes: 1

Views: 866

Answers (3)

auco
auco

Reputation: 9579

Not sure if this will help in your scenario, but if you would like the upper view to ignore any user inputs (touch events), then you can call:

self.pieceX.userInteractionEnabled = NO;

Of course you could also switch this state in the corresponding UIGestureRecognizer delegate method.

Upvotes: 0

Kai Huppmann
Kai Huppmann

Reputation: 10775

This seems to be a job for -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event. Write a subclass of UIView (MyLView) which overrides this method and returns YES if the touch was inside the actual "L".

So if your view is 90. x 120. and the "L" is 30. wide like this:

x00
x00
x00
xxx 

Your implementation could look like this:

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
  return point.x < 30. || point.y > 90.; 
}

Never tested this, but this is how it reads from apple's docs.

Upvotes: 3

Oscar Gomez
Oscar Gomez

Reputation: 18488

By default if a subview cannot respond to a touch it will go thru the responder chain, which in this case would mean the superview, therefore I would recommend making the smaller views subviews of the superview so that there is no "blocking" on the gestures you need.

Upvotes: 0

Related Questions