2014-04-21 117 views
3

我想禁用整個視圖的用戶交互,但也允許在視圖上點按手勢。假設主視圖上方有一個菜單,並且如果用戶再次點擊主視圖,則後菜單消失。但是,只要顯示菜單,則應禁用與主視圖的所有其他交互。 這裏是我嘗試做的是: 我定義的tapGestureRecognizer這樣的:禁用用戶交互但在UIViewController中允許點按手勢

@interface GroupMasterViewController() <SWRevealViewControllerDelegate> 

@property NSMutableArray *groups; 
@property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer; 

@end 

在我viewDidLoad方法我有這樣的:

self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.revealViewController action:@selector(rightRevealToggle:)]; 
[self.view addGestureRecognizer:self.tapGestureRecognizer]; 
self.tapGestureRecognizer.enabled = NO; 

而且在委託方法,被解僱時,菜單打開或關閉:

- (void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position 
{ 
    if (position == FrontViewPositionLeftSide) { 
     self.tapGestureRecognizer.enabled = YES; 
     self.view.userInteractionEnabled = NO; 
     self.tabBarController.tabBar.userInteractionEnabled = NO; 
    } 
    else if (position == FrontViewPositionLeft){ 
     self.tapGestureRecognizer.enabled = NO; 
     self.view.userInteractionEnabled = YES; 
     self.tabBarController.tabBar.userInteractionEnabled = YES; 

    } 
} 

問題是,tapGestureRecognizer不再工作了,如果我禁用用戶交互。

非常感謝您的幫助。

+0

要禁用整個視圖,以便UITapGestureRecognizer不會work.Please使意見的主視圖的子視圖,並啓用和禁用你想 – Rose

回答

4

如果您在禁用用戶交互的同時嘗試啓用tapGestureRecogizer將會產生衝突,因此,我認爲這是最直截了當的解決方案:與其關閉整個視圖的用戶交互,您應該對每個用戶交互的意見,你想不響應觸摸。例如:

- (void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position 
{ 
    if (position == FrontViewPositionLeftSide) { 
     self.tapGestureRecognizer.enabled = YES; 
     self.button1.userInteractionEnabled = NO; 
     self.button2.userInteractionEnabled = NO; 
     self.tabBarController.tabBar.userInteractionEnabled = NO; 
    } 
    else if (position == FrontViewPositionLeft){ 
     self.tapGestureRecognizer.enabled = NO; 
     self.button1.userInteractionEnabled = YES; 
     self.button2.userInteractionEnabled = YES; 
     self.tabBarController.tabBar.userInteractionEnabled = YES; 

    } 
} 
+0

認爲你會怎麼做,如果你有一個表格視圖,如果用戶點擊該表格視圖,它應該關閉菜單。如果我禁用表格視圖的用戶交互,那麼水龍頭不再工作。 – andreaspfr

+0

您只需要禁用表視圖上的交互,但將手勢識別器添加到視圖控制器的主視圖,即'self.view'。只是這樣你仍然可以在不影響表格視圖的情況下點擊視圖。 –

1

我認爲不可能禁用用戶交互,然後期望一些用戶交互事件。您可能會針對特定事件顯示progress bar以阻止用戶互動。