简体   繁体   English

我如何禁用touchesBegan:多点触摸?

[英]how do i disable touchesBegan: for multi-touches?

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

I am currently getting 1 object in 我目前正在获得1个对象

touches 

when I do a tap with two fingers simultaneously (holding option key and clicking on the simulator). 当我用两根手指同时敲击时(按住选项键并点击模拟器)。 I believe this is because I haven't enabled the 我相信这是因为我没有启用

multipleTouchEnabled 

property of the attached view. 所附视图的属性。 I want to make it so that I don't register this event for multi-touches. 我想这样做,以便我不会为多点触摸注册此事件。

Looking into the issue, it seems like it would work if I enable multipleTouchEnabled, and then do 看看这个问题,如果我启用了multipleTouchEnabled,那么它似乎会起作用

if ([touches count] > 1) {
    return;
}

in my 在我的

touchesBegan:

However, this seems strange to me in that I am ENABLING multipleTouchEnabled to DISABLE multiple touches, and am worried if there will be side-effects. 然而,这对我来说似乎很奇怪,因为我正在启用multipleTouchEnabled以禁用多个触摸,并且担心是否会有副作用。 Is there a better way to solve my problem? 有没有更好的方法来解决我的问题?

You should just be able to disable the multitouch property on the view , in IB you have to actually go over to the side panel and click on the thing that says view next to files owner to get it and then uncheck it, or you could do it in code in viewdidload: 您应该只能在视图上禁用多点触控属性,在IB中您必须实际转到侧面板并单击文件所有者旁边的视图以获取它然后取消选中它,或者您可以执行此操作它在viewdidload中的代码中:

self.view.multiTouchEnabled = NO; self.view.multiTouchEnabled = NO;

First add a gesture recognizer to your view: 首先在您的视图中添加手势识别器:

UITapGestureRecognizer *multipleTouches = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleMultiTap:)];
multipleTouches.numberOfTouchesRequired = 2;
[yourViewName addGestureRecognizer:multipleTouches];
[multipleTouches release];

Hope it will help you. 希望它会对你有所帮助。

You could use a gesture recognizer, this is probably more readbale than the touches delegate even if your code works: 你可以使用一个手势识别器,即使你的代码工作,这可能比触摸委托更多readbale:

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myAction:)];
gestureRecognizer.numberOfTouchesRequired = 2;

If it is compulsory to use NSTouches then you can use the following methods to get the desired task achieve. 如果必须使用NSTouches,那么您可以使用以下方法来实现所需的任务。

 [view setMultipleTouchEnabled:NO]; [view setExclusiveTouch:YES]; 

more over you can play with 你可以玩更多

setMultipleTouchEnabled: setMultipleTouchEnabled:

by keeping 通过保持

view's setExclusiveTouch to YES view的setExclusiveTouch为YES

您可以使用此行来避免某个点的多次触摸,为您的视图设置独占触摸“是”。

[self.view setExclusiveTouch:YES];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM