如何禁用多点触控?

我的应用程序有几个button触发不同的事件。 用户不应该能够按住几个button。 总之,按住几个button会崩溃应用程序。

所以,我试图在我的应用程序中禁用多点触摸。

我没有选中所有xib文件中的“Multiple Touch”,只要我能解决这个问题,属性“multipleTouchEnabled”和“exclusiveTouch”控制视图是否使用多点触控。 所以在我的applicationDidFinishLaunching我把这个:

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

在我的每个视图控制器中,我把它放在viewDidLoad中

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

但是,它仍然接受多个触摸。 我可以做一些事情,如禁用其他button后,得到一个触摸事件,但这将是一个丑陋的黑客。 当然有一种方法可以正确禁用多点触摸?

如果一次只需要一个button来响应触摸,则需要为该button设置exclusiveTouch,而不是针对父视图。 或者,您可以在button出现“Touch Down”事件时禁用其他button。


这是后者的一个例子,在我的testing中效果更好。 为button设置exclusiveTouchtypes的工作,但是当你的手指离开button的边缘,而不是只是点击它,导致一些有趣的问题。

您需要将控制器中的sockets连接到每个button上,并将控制器中正确方法的“Touch Down”,“Touch Up Inside”和“Touch Up Outside”事件挂接到适当的方法上。

 #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 
 - (void)viewDidLoad { [super viewDidLoad]; for(UIView* v in self.view.subviews) { if([v isKindOfClass:[UIButton class]]) { UIButton* btn = (UIButton*)v; [btn setExclusiveTouch:YES]; } } } 
 - (void)viewDidLoad { [super viewDidLoad]; for(UIView* v in self.view.subviews) { if([v isKindOfClass:[UIButton class]]) { UIButton* btn = (UIButton*)v; [btn setExclusiveTouch:YES]; } } } 

这段代码经过testing和完美的工作,一次按多个button,没有应用程序崩溃。

你的应用程序崩溃的原因。 进一步调查,使用debugging器,看看有什么不对,而不是试图隐藏错误。

编辑:

好吧,我不得不承认我有点苛刻。 您必须在每个button上设置exclusiveTouch属性。 就这样。 multipleTouchEnabled属性是不相关的。

基于neoevoke的答案,只有改善一点,以便它也检查子视图的孩子,我创build了这个function,并将其添加到我的实用程序文件:

 // Set exclusive touch to all children + (void)setExclusiveTouchToChildrenOf:(NSArray *)subviews { for (UIView *v in subviews) { [self setExclusiveTouchToChildrenOf:v.subviews]; if ([v isKindOfClass:[UIButton class]]) { UIButton *btn = (UIButton *)v; [btn setExclusiveTouch:YES]; } } } 

然后,简单地调用:

 [Utils setExclusiveTouchToChildrenOf:self.view.subviews]; 

…会做的伎俩。

我们的testing人员经常会遇到这个问题。 我有时使用的方法之一,虽然它应该有意识地使用,是为UIView创build类别,如下所示:

 @implementation UIView (ExclusiveTouch) - (BOOL)isExclusiveTouch { return YES; } 

在SWIFT中禁用多点触控:

你需要首先有一个每个button的出口,然后设置独家触摸为true。因此,在你viewDidLoad()将具有:

yourButton.exclusiveTouch = true

//没有必要,但是你也可以添加:

self.view.multipleTouchEnabled = false

非常简单,你可以在这种情况下使用ExclusiveTouch属性

 [youBtn setExclusiveTouch:YES]; 

这是一个布尔值,指示接收方是否专门处理触摸事件。

将此属性设置为YES会导致接收方在同一个窗口中阻止将触摸事件传递到其他视图。 该属性的默认值是NO。

我的经验是,默认情况下,一个新的项目甚至不允许多点触摸,你必须打开它。 但我想这取决于你如何开始。 你有没有使用mutlitouch的例子作为模板?

首先,你是否确定多点触控? 可以很快产生顺序的单一触摸。 多点触控更多的是当你用两个或更多的手指在表面上做什么。 也许你有一个单一的触摸,但没有正确处理如果两个button几乎同时按下会发生什么。

我刚刚有这个问题。

我们提出的解决scheme是简单地从UIButtoninheritance一个覆盖initWithCoder方法的新类,并使用一次只需要一个button(即无处不在)的地方:

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

请注意,这只适用于从nib文件加载的button。

我创build了UIView类扩展,并添加了这两个function。 当我想禁用视图触摸我只是打电话[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]; } 

如果要以编程方式禁用多点触控,或者如果使用的是cocos2d(无multipleTouchEnabled选项),则可以在ccTouches委托上使用以下代码:

 - (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSSet *multiTouch = [event allTouches]; if( [multiTouch count] > 1) { return; } else { //else your rest of the code } 

“Touch Down”事件中禁用视图中的所有button,并在“Touch Up Inside”事件中启用它们。

例如

 - (void) handleTouchDown { for (UIButton *btn in views) { btn.enable = NO; } } - (void) handleTouchUpInside { for (UIButton *btn in views) { btn.enable = Yes; } ------ ------ } 

我通过这种方式决定了这个问题:

 NSTimeInterval intervalButtonPressed; - (IBAction)buttonPicturePressed:(id)sender{ if (([[NSDate date] timeIntervalSince1970] - intervalButtonPressed) > 0.1f) { intervalButtonPressed = [[NSDate date] timeIntervalSince1970]; //your code for button } } 

只需将所有相关的UIView属性exclusiveTouch设置为false即可。

在视图周围拖动对象时,我曾遇到过一些奇怪的情况,如果在同一时间触摸另一个对象,则会触发touchesBegan方法。 我的解决方法是禁用父视图的用户交互,直到touchesEnded或touchesCancelled被调用。

 override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { // whatever setup you need self.view.userInteractionEnabled = false } override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { // whatever setup you need self.view.userInteractionEnabled = true } override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) { // whatever setup you need self.view.userInteractionEnabled = true } 
Interesting Posts