簡體   English   中英

為什么UIView.exclusiveTouch不起作用?

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

在我的一個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:-即使其中一個按鈕的ExclusiveTouch設置為YES。 我嘗試注釋多行觸控行,但無濟於事。 有人可以向我解釋我在這里缺少什么嗎?

謝謝,以利

《 iPhone OS編程指南》中

將事件傳遞限制為單個視圖:

默認情況下,視圖的ExclusiveTouch屬性設置為NO。 如果將屬性設置為YES,則會標記視圖,以便在跟蹤觸摸時它是窗口中唯一在跟蹤觸摸的視圖。 窗口中的其他視圖無法接收這些觸摸。 但是,標記為“排他性觸摸”的視圖不會接收與同一窗口中其他視圖相關聯的觸摸。 如果手指接觸到獨占觸摸視圖,則只有在該視圖是該窗口中唯一跟蹤手指的視圖時,才傳遞該觸摸。 如果手指觸摸到非獨占視圖,則只有在獨占觸摸視圖中沒有其他手指在跟蹤時,才傳遞該觸摸。

它指出獨占觸摸屬性不會影響視圖框架之外的觸摸。

過去,我使用主視圖來跟蹤屏幕上的所有觸摸,而不是讓每個子視圖跟蹤觸摸。 最好的方法是:

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