为什么UIView.exclusiveTouch不起作用?

eli*_*ego 13 iphone cocoa-touch objective-c uikit uiview

在我的一个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);
}
Run Code Online (Sandbox Code Playgroud)

当运行它时,hej:在按下任何按钮时被不同的发送者调用 - 即使其中一个按钮设置为YES.我试过评论multipleTouchEnabled-lines,但没有用.有人可以向我解释我在这里失踪了吗?

谢谢,Eli

Cor*_*oyd 18

来自iPhone OS编程指南:

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

默认情况下,视图的exclusiveTouch属性设置为NO.如果将属性设置为YES,则标记视图,以便在跟踪触摸时,它是窗口中唯一跟踪触摸的视图.窗口中的其他视图无法接收这些触摸.但是,标记为"独占触摸"的视图不会接收与同一窗口中的其他视图关联的触摸.如果手指接触专用触摸视图,则仅当该视图是跟踪该窗口中的手指的唯一视图时才传递该触摸.如果手指触摸非独占视图,则仅当在专用触摸视图中没有另一个手指跟踪时才传递该触摸.

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

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

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