简体   繁体   English

为什么UIView.exclusiveTouch不起作用?

[英]Why doesn't UIView.exclusiveTouch work?

In one of my iPhone projects, I have three views that you can move around by touching and dragging. 在我的一个iPhone项目中,我具有三个视图,您可以通过触摸和拖动来移动它们。 However, I want to stop the user from moving two views at the same time, by using two fingers. 但是,我希望通过使用两个手指来阻止用户同时移动两个视图。 I have therefore tried to experiment with UIView.exclusiveTouch, without any success. 因此,我尝试使用UIView.exclusiveTouch进行试验,但未成功。

To understand how the property works, I created a brand new project, with the following code in the 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);
}

When running this, hej: gets called, with different senders, when pressing any of the buttons - even though one of them has exclusiveTouch set to YES. 运行此命令时,按下任意按钮时,会使用不同的发件人调用hej:-即使其中一个按钮的ExclusiveTouch设置为YES。 I've tried commenting the multipleTouchEnabled-lines, to no avail. 我尝试注释多行触控行,但无济于事。 Can somebody explain to me what I'm missing here? 有人可以向我解释我在这里缺少什么吗?

Thanks, Eli 谢谢,以利

From The iPhone OS Programming Guide : 《 iPhone OS编程指南》中

Restricting event delivery to a single view: 将事件传递限制为单个视图:

By default, a view's exclusiveTouch property is set to NO. 默认情况下,视图的ExclusiveTouch属性设置为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. 如果将属性设置为YES,则会标记视图,以便在跟踪触摸时它是窗口中唯一在跟踪触摸的视图。 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. 如果手指触摸到非独占视图,则只有在独占触摸视图中没有其他手指在跟踪时,才传递该触摸。

It states that the exclusive touch property does NOT affect touches outside the frame of the view. 它指出独占触摸属性不会影响视图框架之外的触摸。

To handle this in the past, I use the main view to track ALL TOUCHES on screen instead of letting each subview track touches. 过去,我使用主视图来跟踪屏幕上的所有触摸,而不是让每个子视图跟踪触摸。 The best way is to do: 最好的方法是:

if(CGRectContainsPoint(thesubviewIcareAbout.frame, theLocationOfTheTouch)){
    //the subview has been touched, do what you want
}

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

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