为什么UIView.exclusiveTouch不起作用?

时间:2009-05-09 13:33:31

标签: objective-c iphone cocoa-touch uiview uikit

在我的一个iPhone项目中,我有三个视图可以通过触摸和拖动来移动。但是,我想通过使用两个手指阻止用户同时移动两个视图。因此,我试图尝试使用UIView.exclusiveTouch,但没有任何成功。

为了理解该属性的工作原理,我创建了一个全新的项目,在视图控制器中使用以下代码:

- (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:在按下任何按钮时会被不同的发送者调用 - 即使其中一个按钮设置为YES。我试过评论multipleTouchEnabled-lines,但没有用。有人可以向我解释我在这里缺少什么吗?

谢谢, 利

1 个答案:

答案 0 :(得分:18)

来自The iPhone OS Programming Guide

  

将事件传递限制为单个视图:

     

默认情况下,视图的exclusiveTouch属性设置为NO。如果你设置   如果属性为YES,则标记视图,以便进行跟踪   触摸,它是窗口中唯一跟踪触摸的视图。   窗口中的其他视图无法接收这些触摸。但是,一个   标记为“独占触摸”的视图不会接收到触摸   与同一窗口中的其他视图相关联。如果一个手指   联系独家触摸视图,然后只有在触摸时才会发送   该视图是在该窗口中跟踪手指的唯一视图。如果一个   手指触摸非独占视图,然后仅触发该触摸   如果在独家触摸视图中没有其他手指跟踪。

它声明独有的触摸属性不会影响视图框架外的触摸。

为了解决这个问题,我使用主视图在屏幕上跟踪所有触摸,而不是让每个子视图轨道都接触。最好的方法是:

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