2

是否可以在視圖中實現手勢識別器並將其傳播到所有其他UI組件? 如果我做這樣的事情,這是行不通的:將手勢識別器應用於所有視圖組件?

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)]; 
    swipe.direction = UISwipeGestureRecognizerDirectionRight; 

    [self.view addGestureRecognizer:swipe]; 
    [TitleLabel addGestureRecognizer:swipe]; 
    [DescLabel addGestureRecognizer:swipe]; 
    [_TopView addGestureRecognizer:swipe]; 
    [_BottomView addGestureRecognizer:swipe]; 
    [_ScrollView addGestureRecognizer:swipe]; 
    [_TableView addGestureRecognizer:swipe]; 

    [swipe release]; 

我該怎麼辦呢?

我需要在我的視圖上添加一個透明視圖,該視圖涵蓋所有對象? 還是有一個聰明的方法來做到這一點?

回答

0

你的發現是正確的回覆:相同的識別器,其多個組件,然而,爲了完整起見...

NSInteger count = 1; 
NSInteger total = [[self.view subviews] count]; 
for (id obj in [self.view subviews]) 
{ 
    NSLog(@"testing object %i of %i", count, total); 
    count++; 

    if ([obj respondsToSelector:@selector(addGestureRecognizer:)]) 
    { 
     UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)]; 
     swipe.direction = UISwipeGestureRecognizerDirectionRight; 
     [obj addGestureRecognizer: swipe]; 
     [swipe release]; 
     NSLog(@"swipe added"); 
    } 
} 

我預見是,如果任何對象要應用的識別器的唯一問題,嵌入在更多的視圖中,這些視圖已經是self.view的子視圖。然後你需要檢查self.view找到的子視圖是否爲類型UIView,如果是,然後遍歷該視圖的子視圖等。

+0

是的,你有權利。 – elp