Domanda

I'm designing a game that takes place on a grid. The grid needs to respond to pan gestures that begin in one square and end in another. I have a custom view controller class called GameVC which contains the grid of UIViews of subclass GameGridSquare. I want the game to perform an action when, for example, a pan gesture begins and ends in neighboring squares. I have a storyboard wired with properties that name each square by row and column: self.A1, self.A2, ... self.H7, self.H8. As a pan gesture is recognized, I want the GameVC to receive the two GameGridSquares where UIGestureRecognizerStateBegan and UIGestureRecognizerStateEnded so it can determine the appropriate action, like so:

-(void)validatePanGestureFrom:(GameGridSquare*)beginSquare 
                           to:(GameGridSquare*)endSquare

What's the best way to do this? My recognizers are functional and returning coordinates, but I think I need to explore hit-testing.

If I add recognizers to the individual GameGridSquares, I get undesired results. For example, a pan that begins in A1 and ends in B2 would be recognized by A1 alone. This suggests that I need a pan recognizer on GameVC that can detect when the gesture begins and ends in separate subviews.

From what I've read, I believe that the UIGestureRecognizerDelegate protocol may be helpful here. I also understand that a custom UIPanGestureRecognizer subclass would allow me to override hitTest:withEvent but I'm not sure where to even begin with that. Any ideas about how I should approach this?

È stato utile?

Soluzione

This was what I was after:

Find which child view was tapped when using UITapGestureRecognizer

UIView* view = gestureRecognizer.view;
CGPoint loc = [gestureRecognizer locationInView:view];
UIView* subview = [view hitTest:loc withEvent:nil];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top