gpt4 book ai didi

objective-c - 为什么 UIView.exclusiveTouch 不起作用?

转载 作者:太空狗 更新时间:2023-10-30 03:20:22 24 4
gpt4 key购买 nike

在我的一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com