- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在我的一个 iPhone 项目中,我有三个 View ,您可以通过触摸和拖动来移动它们。但是,我想阻止用户使用两根手指同时移动两个 View 。因此,我尝试使用 UIView.exclusiveTouch 进行试验,但没有成功。
为了了解该属性的工作原理,我创建了一个全新的项目,在 View Controller 中使用以下代码:
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
UIButton* a = [UIButton buttonWithType:UIButtonTypeInfoDark];
[a addTarget:self action:@selector(hej:) forControlEvents:UIControlEventTouchUpInside];
a.center = CGPointMake(50, 50);
a.multipleTouchEnabled = YES;
UIButton* b = [UIButton buttonWithType:UIButtonTypeInfoDark];
[b addTarget:self action:@selector(hej:) forControlEvents:UIControlEventTouchUpInside];
b.center = CGPointMake(200, 50);
b.multipleTouchEnabled = YES;
a.exclusiveTouch = YES;
[self.view addSubview:a];
[self.view addSubview:b];
}
- (void)hej:(id)sender
{
NSLog(@"hej: %@", sender);
}
运行此程序时,hej: 会在按下任何按钮时被不同的发件人调用 - 即使其中一个按钮将 exclusiveTouch 设置为 YES。我试过评论 multipleTouchEnabled 行,但无济于事。有人可以向我解释我在这里缺少什么吗?
谢谢,以利
最佳答案
来自 The iPhone OS Programming Guide :
Restricting event delivery to a single view:
By default, a view’s exclusiveTouch property is set to NO. If you set the property to YES, you mark the view so that, if it is tracking touches, it is the only view in the window that is tracking touches. Other views in the window cannot receive those touches. However, a view that is marked “exclusive touch” does not receive touches that are associated with other views in the same window. If a finger contacts an exclusive-touch view, then that touch is delivered only if that view is the only view tracking a finger in that window. If a finger touches a non-exclusive view, then that touch is delivered only if there is not another finger tracking in an exclusive-touch view.
它声明独占触摸属性不会影响 View 框架之外的触摸。
过去为了处理这个问题,我使用主视图来跟踪屏幕上的所有触摸,而不是让每个 subview 跟踪触摸。最好的方法是:
if(CGRectContainsPoint(thesubviewIcareAbout.frame, theLocationOfTheTouch)){
//the subview has been touched, do what you want
}
关于objective-c - 为什么 UIView.exclusiveTouch 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/843338/
我正在尝试将 exclusiveTouch 属性设置为我的自定义 UITableViewCell。 但是,添加这段代码似乎不起作用。 for (UIView *view in cell.subview
我正在启动一个带有 textField 的简单 UIView - 让我们称之为 orderSetNameView - 点击按钮即可。我希望使该 View 成为模态视图,但不使用 [UIViewCont
在 iOS 7 上,当 UIAlertView 出现时,它的按钮似乎自动设置了 exclusiveTouch=YES。这样一来,如果您在警报 View 中同时按下多个按钮,就不会发生什么可怕的事情。
我有两个 UIView,里面装满了 UIButton。我只想允许一个按钮一次接受每个 UIView 的触摸。也就是说,如果您在每组按钮中有一个手指,我希望多点触控工作,但如果您在同一组中有两个手指,则
我设置了 UIButton的独家联系 YES ,并且工作正常(如果我触摸按钮,其他 View 将不会收到触摸事件)。异常(exception)是当我触摸 UIBarButtonItem在导航栏上,它的
我试图忽略添加到我的自定义 UITableViewCell 子类的某些 subview 的触摸。我将该 subview 的 exclusiveTouch 设置为 YES,但触摸仍会触发单元格选择。有没
在我的一个 iPhone 项目中,我有三个 View ,您可以通过触摸和拖动来移动它们。但是,我想阻止用户使用两根手指同时移动两个 View 。因此,我尝试使用 UIView.exclusiveTou
我确信有人希望有多个按钮同时接受触摸的原因有很多。然而,我们大多数人一次只需要按下一个按钮(用于导航、以模态方式呈现某些内容、呈现弹出框、 View 等)。 那么,为什么 Apple 将 UIButt
我是一名优秀的程序员,十分优秀!