0

I am writing a card game. When users touch a card in their hand, the card they are touching slides out a little to clarify which one they are touching since they can have up to 18 cards in their hand and they are kind of small. What I want next is for the user to be able to swipe the card up to play it.

I have tried to use the UISwipeGestureRecognizer, but since the user may pause when they first touch the screen (to make sure the right card slides out), it often doesn't recognize it as a swipe. The user may also just slide his finger over to the adjacent card and then swipe. So, that's another issue since gestures are only recognized on the UIView that the touch begins on.

The best I can think of is to use a UIPanGestureRecognizer on the parent view (the UIView that contains all the cards). I can use it's velocity to decide whether or not it should be considered a swipe. I'd have to set cancelsTouchesInView to NO and still just use touchesBegan, etc. to detect which card was swiped. Is there a better way?

dontangg
  • 4,729
  • 1
  • 26
  • 38
  • Instead of making UIPanGesture work in what may not be a normal way. You could make your own custom Swipe gesture that works the way you want by subclassing UIGestureRecognizer. – Ryan Poolos Jun 25 '12 at 15:13
  • I like this idea best. It seems the cleanest. I'm off to watch the WWDC videos on this. Thanks. – dontangg Jun 25 '12 at 17:45
  • I put up an answer with a great link on custom gestures if you're inclined to mark it as right :) – Ryan Poolos Jun 25 '12 at 17:53

2 Answers2

1

If I understand correctly what you are trying to do, I would try by combining a UILongPressureGestureRecognizer and a UISwipeGestureRecognizer.

UILongPressGestureRecognizer is a concrete subclass of UIGestureRecognizer that looks for long-press gestures. The user must press one or more fingers on a view for at least a specified period for the action message to be sent. In addition, the fingers may move only a specified distance for the gesture to be recognized; if they move beyond this limit the gesture fails.

Long-press gestures are continuous. The gesture begins (UIGestureRecognizerStateBegan) when the number of allowable fingers (numberOfTouchesRequired) have been pressed for the specified period (minimumPressDuration) and the touches do not move beyond the allowable range of movement (allowableMovement). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizerStateEnded) when any of the fingers are lifted.

UILongPressGestureRecognizer would allow the user to "pick" the card; if they move the finger too much (i.e., over a contiguous card) the gesture fails.

In the UIGestureRecognizerStateBegan you could set a variable to track the currently "picked" card (as long as it remains such).

If the user then swipes on the parent view (the one containing all the cards), you would move the card.

On the other hand, I have the feeling that this would not be that complex to implement in terms of touchesBegan/touchesMoved/touchesEnded and that this could be the most appropriate way to deal with it.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
1

As I said in my comment, Instead of making UIPanGesture work in what may not be a normal way, You could make your own custom Swipe gesture that works the way you want by subclassing UIGestureRecognizer.

Check out this link. The first part is about general Gestures but the second half is about making a custom gesture.

http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more

Ryan Poolos
  • 18,421
  • 4
  • 65
  • 98