How to disable multitouch?

2019-01-08 16:39发布

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?

18条回答
Luminary・发光体
2楼-- · 2019-01-08 17:10

I created UIView Class Extension and added this two functions. and when i want to disable view touch i just call [view makeExclusiveTouch];

- (void) makeExclusiveTouchForViews:(NSArray*)views {
    for (UIView * view in views) {
        [view makeExclusiveTouch];
    }
}

- (void) makeExclusiveTouch {
    self.multipleTouchEnabled = NO;
    self.exclusiveTouch = YES;
    [self makeExclusiveTouchForViews:self.subviews];
}
查看更多
闹够了就滚
3楼-- · 2019-01-08 17:10

For disabling global multitouch in Xamarin.iOS

Copy&Paste the code below:

[DllImport(ObjCRuntime.Constants.ObjectiveCLibrary, EntryPoint = "objc_msgSend")]
internal extern static IntPtr IntPtr_objc_msgSend(IntPtr receiver, IntPtr selector, bool isExclusiveTouch);
static void SetExclusiveTouch(bool isExclusiveTouch)
{
    var selector = new ObjCRuntime.Selector("setExclusiveTouch:");
    IntPtr_objc_msgSend(UIView.Appearance.Handle, selector.Handle, isExclusiveTouch);
}

And set it on AppDelegate:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    ...
    SetExclusiveTouch(true); // setting exlusive to true disables the multitouch
    ...
}
查看更多
疯言疯语
4楼-- · 2019-01-08 17:11

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.

查看更多
乱世女痞
5楼-- · 2019-01-08 17:11

If you want to disable multi touch throughout the application and don't want to write code for each button then you can simply use Appearance of button. Write below line in didFinishLaunchingWithOptions.

UIButton.appearance().isExclusiveTouch = true

Thats great!! UIAppearance

You can even use it for any of UIView class so if you want to disable multi touch for few buttons. Make a CustomClass of button and then

CustomButton.appearance().isExclusiveTouch = true

There is one more advantage which can help you. In case you want to disable multi touch of buttons in a particular ViewController

UIButton.appearance(whenContainedInInstancesOf: [ViewController2.self]).isExclusiveTouch = true
查看更多
在下西门庆
6楼-- · 2019-01-08 17:13

If you want to disable multitouch programmatically, or if you are using cocos2d (no multipleTouchEnabled option), you can use the following code on your ccTouches delegate:

- (BOOL)ccTouchesBegan:(NSSet *)touches
 withEvent:(UIEvent *)event {
       NSSet *multiTouch = [event allTouches];
       if( [multiTouch count] > 1) { 
            return; 
       }
       else {
           //else your rest of the code  
}
查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-01-08 17:16
- (void)viewDidLoad {
    [super viewDidLoad];

    for(UIView* v in self.view.subviews)
    {
        if([v isKindOfClass:[UIButton class]])
        {
            UIButton* btn = (UIButton*)v;
            [btn setExclusiveTouch:YES];
        }
    }
}
查看更多
登录 后发表回答