How to set the maximum duration between two taps in UITapGesture? - iphone

In my iOS app, I am using UITapGesture with numberOfTapsRequired equal to two. But I need to specify the maximum duration required between two taps.
If the duration taken between two taps is greater that the specified (Say 0.5 sec) the gesture should not work.
Please guide me how can I achieve this.
Thanks in advance!

It seems like you dont need to handle the tap gesture by the way of maximum duration for each tap in your gesture. You just need to specify how many touches and taps required and in the method you can check the state of the tap gesture.
- (void)handleTap:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
// handling code
}
}
Above piece of code is from the apple documentation.

This question reminds me of how the Double Tap gesture was implemented before Apple went out and implemented it themselves in UITapGestureRecognizer.
Before all of that, we used the methods, touchesBegan and touchesEnd to keep track of the number of fingers touching the screen and also, add a delay to make sure we track double taps. Thats when we could use the time which you asked. Now, there is simply no need as R.A pointed out.

Related

Detecting a finger being held on an object

I am trying to have an image that when the user touches it, it wiggles and as soon as the user lifts their finger it stops.
Is there a gesture that I can use to detect when the finger is down, not just on the initial touch, or when the user moves there finger?
I have tried a LongPress gesture, but that does not get called the entire time the finger is on the view. Can anyone help me with the best way to active this. Right now i am doing it using touchesBegin, touchesMoved, touchesEnd, but i was wondering if there is a better way.
Any suggestions are greatly appreciated.
Thanks
EDIT
Based on the comments, I slightly misunderstood the original question, so I edit my answer to a different solution, which hopefully is a bit more clear (and answers the actual question - not the one that was in my head).
A LongPress gesture is continuous (where a tap gesture is not). That means, the recognizer callback will continue to be invoked until the gesture is complete - which does not happen until the "longpress" is released. So, the following should do what you want. NOTE: I think you want to "start shaking" a view when the long-press is recognized, then "stop shaking" the view when the fingers are released. I just pretended you have functions for that. Substitute appropriately.
- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
StartShakingView(gestureRecognizer.view);
} else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
StopShakingView(gestureRecognizer.view);
}
}
The Apple Touches sample includes code that demonstrate using both UIResponder and UIGestureRecognizer methods.
Either should work for what you're doing.
Simple answer - you could make the image a UIButton, and start the wiggle on TouchDown, and stop it on TouchUpInside or TouchUpOutside
It sounds like you want to subclass UIGestureRecognizer, which, as I recall, gets the touchesBegan:... and associated methods. Read the notes on subclassing in the UIGestureRecognizer reference. Or use a UIButton as SomaMan suggests.

iphone sdk how to catch a long press after 1 second?

I'm using UILongPressGestureRecognizer to catch a Long Press event. The problem is I can only respond to it after user release his finger.
How do I implement a function that will respond after user hold for x seconds?
UILongPressGestureRecognizer works for this, check your code again and set duration for long press as -
[longPressGesture setMinimumPressDuration:<#(CFTimeInterval)#>];
Instead of using a gesture recognizer, use the UIView instance methods touchesBegan:withEvent:, touchesMoved:withEvent: and touchesEnded:withEvent:. While they're not as convenient as using a gesture recognizer, you have complete control of the touch interpretation.
Note that if you every try to use these to interpret multitouch gestures, the touch screen is "bouncy", in that the reported number of touches will vary during the touch event processing. My app interprets pinches, which are reported to me as a random sequence of one and two finger touches. I managed to debounce it, but getting the code right was a real PITA.

Difficulty activating 3 touch gesture recognizer in iOS app

I have an app that uses gesture recognizers quite a bit. From the studying I have done, I have found that there is the touchesBegan method of recognizing a gesture, and then there are gesture recognizers, which should be more slick.
The problem I am running into is that the gesture recognizers aren't nearly as responsive or accurate as the touchesBegan method, but are a lot easier to implement, which is obviously why I am using them. If I want to have a 3 finger gesture detected with a gesture recognizer, it is quite difficult because I have to press down my 3 fingers at the EXACT same time, or else it won't fire. This is in contrast to the touchesBegan method that just knows how many fingers you have down at any point.
Am I missing something with the implementation of this seemingly nice gesture feature that is making it not very responsive? I have set the max and min touches to 3, is that incorrect?
Please help. Thanks!!
The reason you need to press at the exact same time is because, by default, only one gesture recognizer can be recognized at a time. So once you press one finger down that recognizer automatically blocks the other two.
Try implementing the UIGestureRecognizerDelegate and using:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
I'm not sure if this will solve the issue or not but it might.
Cheers.

How can I find out when a pinch gesture is finished (UIGestureRecognizer)

I want to get a callback when my UIPinchGestureRecognizer finished a pinch-gesture. Moreover it would be great to know if the finished gesture was a zoom in or a zoom out.
Does anyone know a method to use? Or the approach to do?
Thanks!
Another approach instead of overriding touchesEnded:, is that you could just check the state of the gesture recognizer in your target handler method.
-(void)handlePinchGesture:(UIGestureRecognizer*)gestureRecognizer {
if(UIGestureRecognizerStateEnded == [gestureRecognizer state]){
// do something
}
}
You can know if it was a zoom in or out by the scale property of the UIPinchGestureRecognizer.
Just overrride it's touchesEnded: method to get a callback (and the call some other method if you wish).
The best approach which does not require subclassing is to examine the "state" property on the gesture recognized instance in your action handler. The state will change during all phases of the lifecycle of the gesture. The state change you're looking for is UIGestureRecognizerStateEnded. It is also good practice to check for UIGestureRecognizerStateCancelled as well.

How can I sense the user is holding down their finger on the screen without moving?

The best method I can think of is to start a timer in the touchesBegan event method. If the timer expires before the touchesEnded event arrives, then you know the user is holding down on the screen. If the touchesMoved event is called, then simply reset the timer so as to only detect holding down without movement.
Is there any functionality built into the iOS SDK to handle exactly this? Or any better, simpler, faster methods anyone can think of?
Thanks in advance for your help!
Try using the UILongPressGestureRecognizer.
UILongPressGestureRecognizer* gr = [[UILongPressGestureRecognizer alloc]
initWithTarget:theTarget
action:#selector(someAction:)];
// change options of gr if you like.
// default: tolerate movement up to 4 px, fire the event after 0.4 secs.
[theView addGestureRecognizer:gr];
[gr release];
When the user long-pressed [theTarget someAction:gr] will be called.
I don't know of another way to test for no movement; I think how you are doing it would be simple enough.
You will most likely not be able to use touchesMoved to reset the timer since it is very, very sensitive and you move your finger without even being able to see it with the naked eye (feel free to test this with NSLogs to see what I mean).
You may want to implement some type of threshold difference value for how much the touchesMoved value has changed from the original value before you reset the timer.