views:

2437

answers:

4

My app has several buttons which trigger different events. The user should NOT be able to hold down several buttons. Anyhow, holding down several buttons crashes the app.

And so, I'm trying to disable multi-touch in my app.

I've unchecked 'Multiple Touch' in all the xib files, and as far as I can work out, the properties 'multipleTouchEnabled' and 'exclusiveTouch' control whether the view uses multitouch. So in my applicationDidFinishLaunching I've put this:

self.mainViewController.view.multipleTouchEnabled=NO;
self.mainViewController.view.exclusiveTouch =YES;

And in each of my view controllers I've put this in the viewDidLoad

self.view.multipleTouchEnabled=NO;
self.view.exclusiveTouch=YES;

However, it still accepts multiple touches. I could do something like disable other buttons after getting a touch down event, but this would be an ugly hack. Surely there is a way to properly disable multi-touch?

+2  A: 

Your app crashes for a reason. Investigate further, use the debugger, see what's wrong instead of trying to hide the bug.

Edit:

OK, ok, I have to admit I was a bit harsh. You have to set the exclusiveTouch property on each button. That's all. The multipleTouchEnabled property is irrelevant.

Nikolai Ruhe
i'm not trying to hide the bug. the user should not be able to press multiple buttons.
cannyboy
Harsh, yes. But also correct. exclusiveTouch needs to be set on each button, and in this case, with a bug that is likely to be difficult to track down without detailed information... the debugger is going to be your best friend on this one.
mmc
setting exclusiveTouch on each button has no effect at all.
cannyboy
Yes it does. Try debugging at the point where you set exclusiveTouch on the buttons. Are the IBOutlets connected already? If you still don't believe me, try the example project I set up for you: http://rapidshare.de/files/47745816/a.zip.html
Nikolai Ruhe
apologies. the buttons were not connected properly in IB
cannyboy
A: 

My experience is that, by default, a new project doesn't even allow multitouch, you have to turn it on. But I suppose that depends on how you got started. Did you use a mutlitouch example as a template?

First of all, are you absolutely sure multitouch is on? It's possible to generate single touches in sequence pretty quickly. Multitouch is more about what you do with two or more fingers once they are on the surface. Perhaps you have single touch on but aren't correctly dealing with what happens if two buttons are pressed at nearly the same time.

Nosredna
The template I'm using is the standard Utility Application. Yes, perhaps it is 'single touched in sequences' I should be worried about. I need to disable all other buttons when one button is held down. But I'm still no closer to a solution.
cannyboy
+3  A: 

If you want only one button to respond to touches at a time, you need to set exclusiveTouch for that button, rather than for the parent view. Alternatively, you could disable the other buttons when a button gets the "Touch Down" event.


Here's an example of the latter, which worked better in my testing. Setting exclusiveTouch for the buttons kind-of worked, but led to some interesting problems when you moved your finger off the edge of a button, rather than just clicking it.

You need to have outlets in your controller hooked up to each button, and have the "Touch Down", "Touch Up Inside", and "Touch Up Outside" events hooked to the proper methods in your controller.

#import "multibuttonsViewController.h"

@implementation multibuttonsViewController

// hook this up to "Touch Down" for each button
- (IBAction) pressed: (id) sender
{
    if (sender == one)
    {
     two.enabled = false;
     three.enabled = false;
     [label setText: @"One"]; // or whatever you want to do
    }
    else if (sender == two)
    {
     one.enabled = false;
     three.enabled = false;
     [label setText: @"Two"];  // or whatever you want to do
    }
    else
    {
     one.enabled = false;
     two.enabled = false;
     [label setText: @"Three"];  // or whatever you want to do
    }
}

// hook this up to "Touch Up Inside" and "Touch Up Outside"
- (IBAction) released: (id) sender
{
    one.enabled = true;
    two.enabled = true;
    three.enabled = true;
}

@end
Mark Bessey
Does that means putting button1.exclusiveTouch = YES;button2.exclusiveTouch = YES;button3.exclusiveTouch = YES; in the viewDidLoad of the view controller? I've tried that - no effect.
cannyboy
I did just try it, and it worked for me. Is it possible that your viewController outlets aren't set properly? Might be worth checking in the debugger.
Mark Bessey
apologies. the buttons were not connected properly in IB
cannyboy
A: 

I've just had exactly this problem.

The solution we came up with was simply to inherit a new class from UIButton that overrides the initWithCoder method, and use that where we needed one button push at a time (ie. everywhere):

@implementation ExclusiveButton

(id)initWithCoder: (NSCoder*)decoder 
{ 
   [self setExclusiveTouch:YES]; 
   return [super initWithCoder:decoder]
}

@end

Note that this only works with buttons loaded from nib files.

CdrJameson