hello there, in my app i have one UIButton, created by the interface builder.
its connected by a IBAction, the one which does the sounds functions.
i want it to have a different behavior when there is a double touch(2 fingers at the same time) , or multiple touch, and in case of single touches, it hass the normal behavier.
what is the best way to do this?
Calling in IBAction the function
- (void)touchesBeganNSSet *)touches withEventUIEvent *)event
Or what?
Thanks a lot!
-(IBAction) snare: (id)sender{
// if (tapCount >= 2) {
// NSLog(@"2 fingers or more, at the same time");
// }
//else{ }
Use a UITapGestureRecognizer. Look at the docs if you are confused. Still confused, then ask. If you do ask, ask specific questions, not "I don't know how to do this. Can you please write some code so that I can copy and paste it and basically get some free code?"
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
Use a UITapGestureRecognizer. Look at the docs if you are confused. Still confused, then ask. If you do ask, ask specific questions, not "I don't know how to do this. Can you please write some code so that I can copy and paste it and basically get some free code?"
i readed about UITapGestureRecognizer but didn`t understand exactly.
I have to use UITapGestureRecognizer inside my IBAction function?
I`m sorry... i`m newbie on this !
You use UIGestureRecognizers by creating them, adding a target and action pair to it, and attaching it to the view (your button). A UITapGestureRecognizer has certain properties you can set such as numberOfTaps and numberOfTouches.
To do all these things, look at the methods defined in the docs.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
hello there, in my app i have one UIButton, created by the interface builder.
its connected by a IBAction, the one which does the sounds functions.
i want it to have a different behavior when there is a double touch(2 fingers at the same time) , or multiple touch, and in case of single touches, it hass the normal behavier.
what is the best way to do this?
Calling in IBAction the function
- (void)touchesBeganNSSet *)touches withEventUIEvent *)event
Or what?
Thanks a lot!
-(IBAction) snare: (id)sender{
// if (tapCount >= 2) {
// NSLog(@"2 fingers or more, at the same time");
// }
//else{ }
You don't call touchesBegan. You write a touchesBegan method, and the system calls it when it detects touches on your object. You could write your own touchesBegan method to detect double-taps, but I would recommend against that for a beginner.
As the other poster said, you could also create a UIGestureRecognizer and attach it to your button. However, that's probably a little beyond your current abilities, based on the nature of the questions you're asking.
There's actually a much simpler way.
When you connect your button to your action in interface builder, connect it to the "touch down repeat" event instead of the usual "touch up inside." Your action will only get called if the tap count is greater than 1.
If you want to detect only double-taps, not triple (or greater) numbers of taps, it gets a little more complicated.
You need to write your IBAction method with an extra parameter, the event that triggers it. The IBAction should take this form:
Then you can check the tapCount property of the event that is passed in to your IBAction. If tapCount == 2, the event was a double-tap. Otherwise, you just ignore the event.
Your snare event might look like this:
Code:
- (void)snare:(id)sender forEvent:(UIEvent *)event
{
if (event.tapCount !=2)
return;
//At this point, we know we got a double-tap. Do whatever we need to do.
}
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
You don't call touchesBegan. You write a touchesBegan method, and the system calls it when it detects touches on your object. You could write your own touchesBegan method to detect double-taps, but I would recommend against that for a beginner.
As the other poster said, you could also create a UIGestureRecognizer and attach it to your button. However, that's probably a little beyond your current abilities, based on the nature of the questions you're asking.
There's actually a much simpler way.
When you connect your button to your action in interface builder, connect it to the "touch down repeat" event instead of the usual "touch up inside." Your action will only get called if the tap count is greater than 1.
If you want to detect only double-taps, not triple (or greater) numbers of taps, it gets a little more complicated.
You need to write your IBAction method with an extra parameter, the event that triggers it. The IBAction should take this form:
Then you can check the tapCount property of the event that is passed in to your IBAction. If tapCount == 2, the event was a double-tap. Otherwise, you just ignore the event.
Your snare event might look like this:
Code:
- (void)snare:(id)sender forEvent:(UIEvent *)event
{
if (event.tapCount !=2)
return;
//At this point, we know we got a double-tap. Do whatever we need to do.
}
Hey Duncan! Thanks for the tips and the reply !
what i want to do its something like that:
i have a button connected by ibaction, and i put what you say within a extra parameter, the event.
I want IF i press with one finger, the sounds = "A"
ELSE the sound = "B"
i did what you say, but now i don`t use touchesBegan, tapCount it`s not defined. and i think tapcount it`s used to tell how many touches would be repeated, and not how many fingers, right?
I need the numbers os fingers at the same time
If one finger sound = a
if two or more fingers sound = b
what i want to do its something like that:
i have a button connected by ibaction, and i put what you say within a extra parameter, the event.
I want IF i press with one finger, the sounds = "A"
ELSE the sound = "B"
i did what you say, but now i don`t use touchesBegan, tapCount it`s not defined. and i think tapcount it`s used to tell how many touches would be repeated, and not how many fingers, right?
I need the numbers os fingers at the same time
If one finger sound = a
if two or more fingers sound = b
Thanks a lot !!
Oops. Sorry, I misunderstood. Yes, I was talking about double-tapping with a single finger.
The other poster has the right idea using a UITapGestureRecognizer.
You should be able to create a pair of UITapGestureRecognizer objects and attach them to your button. set the numberOfTouchesRequired on one of the recognizers to 2. (the default is 1 tap.)
Add a target and action to each one. You can use a separate action method for each so you can tell them apart, or use the same method, and check the numberOfTouches method of the sender in the action method.
Disclaimer: I've barely used gesture recognizers. The above is from a quick survey of the docs. (Most of the code I've written has been for any device with iOS 3.0 or later, so I've tended to steer away from 4.0 APIs until recently.)
I did a little digging in the docs, and found a sample app included in Xcode called SimpleGestureRecognizers. I modified it to attach 2 tap recognizers to a view instead of one, as I described above. It worked. In my case, I checked numberOfTouches in the action method, and used that to tell if it was a 1-finger or 2-finger touch.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
You scared me there for a second Duncan. I am using UIGestureRecognizer for iOS 3.2 and later and you said it was 4.0 API. For reference to anybody that views this thread, all the current UIGestureRecognizers work in iOS 3.2 and later.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
You scared me there for a second Duncan. I am using UIGestureRecognizer for iOS 3.2 and later and you said it was 4.0 API. For reference to anybody that views this thread, all the current UIGestureRecognizers work in iOS 3.2 and later.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Oops. Sorry, I misunderstood. Yes, I was talking about double-tapping with a single finger.
The other poster has the right idea using a UITapGestureRecognizer.
You should be able to create a pair of UITapGestureRecognizer objects and attach them to your button. set the numberOfTouchesRequired on one of the recognizers to 2. (the default is 1 tap.)
Add a target and action to each one. You can use a separate action method for each so you can tell them apart, or use the same method, and check the numberOfTouches method of the sender in the action method.
Disclaimer: I've barely used gesture recognizers. The above is from a quick survey of the docs. (Most of the code I've written has been for any device with iOS 3.0 or later, so I've tended to steer away from 4.0 APIs until recently.)
I did a little digging in the docs, and found a sample app included in Xcode called SimpleGestureRecognizers. I modified it to attach 2 tap recognizers to a view instead of one, as I described above. It worked. In my case, I checked numberOfTouches in the action method, and used that to tell if it was a 1-finger or 2-finger touch.
Where i have to create a pair of UITapGestureRecognizer objects?? And what i should do with the action attached in this object? i started something, but dont know what do for sure...
Where i have to create a pair of UITapGestureRecognizer objects?? And what i should do with the action attached in this object? i started something, but dont know what do for sure...
The singleTap gesture recognizer will detect single taps on your button and invoke the handleSingleTap action, and the doubleTap gesture recognizer will double single taps on your button and invoke the handleDoubleTap action.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
The singleTap gesture recognizer will detect single taps on your button and invoke the handleSingleTap action, and the doubleTap gesture recognizer will double single taps on your button and invoke the handleDoubleTap action.
Make sense?
Yeah make a lot of sense! hahah
i made with requireGestureRecognizerToFail, but if don`t need i`ll try without. It`s working, and i put with CGrect to this worked in a especific area!
But if i need to put, 2 fingers or more? how to do??
Yeah make a lot of sense! hahah
i made with requireGestureRecognizerToFail, but if don`t need i`ll try without. It`s working, and i put with CGrect to this worked in a especific area!
But if i need to put, 2 fingers or more? how to do??
thanks A LOT !!!
Sorry, I don't understand what you just said.
The code I posted creates 2 tap gesture recognizers. The first one responds to a single finger tap. The second one responds to a two-finger tap. So I already told you how to handle a one-finger and a two-finger tap.
The gesture recognizers invoke separate actions.
If you need to handle a 3-finger tap, create a tap recognizer and set it's numberOfTouchesRequired to 3. It will invoke it's action method when it detects a 3-finger tap.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
The code I posted creates 2 tap gesture recognizers. The first one responds to a single finger tap. The second one responds to a two-finger tap. So I already told you how to handle a one-finger and a two-finger tap.
The gesture recognizers invoke separate actions.
If you need to handle a 3-finger tap, create a tap recognizer and set it's numberOfTouchesRequired to 3. It will invoke it's action method when it detects a 3-finger tap.
huuum ok !
yeah, I would like to put 2 or more fingers... is there any way to do it like this? or just create a tap recognizer for every finger ??
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.