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.
Related
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.
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.
i have UISwipeGestureRecognizer that detects if the user has swiped. i also draw the users touches using the touches moved method found in the tutorial below.
http://www.ifans.com/forums/showthread.php?t=132024
My issue is the gesture seems to override my drawing. is it possible to do both or is there a better way of doing this?
Thanks
Dan
All of the touches methods get overridden when you invoke a gestureRecognizer. You can view it here in the explanation. It may be best to just write your own code to recognize a gesture if you need all of the touch events for drawing updates. If you still want to use a gestureRecognizer, you can use the locationInView: method to find the location of the touch, and then pass those points to your draw method.
I would also think about using OpenglES instead, as it isa bit easier, and you can do more things. There's a great sample project about it here. Hope that helps!
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.
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.