2017-02-07 46 views
1

所以我有一個簡單的UIPopoverPresentationController顯示一些內容。關閉任何手勢的UIPopoverPresentationController,而不只是點擊

用戶可以通過點擊屏幕上的任何位置來忽略它(默認彈出式行爲)。

我希望如果用戶在屏幕上做任何類型的水龍頭或手勢,彈出將被解散。最好拖動手勢。

任何想法,如果這是可能的?如何?

回答

1

嘗試使用touchesBegan:withEvent方法

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     if let touch = touches.first { 
      if touch.view == self.view { 
       self.dismiss() 
      } else { 
       return 
      } 

     } 
    } 
+0

感謝您的回答。可悲的是不工作。 touchesBegan永遠不會被調用。 – Guy

0
VC

在酥料餅的呈現的視圖。 在presentViewController:動畫:完成:塊

[self presentViewController:vc animated:YES completion:^{ 
UIView *v1 = vc.view.superview.superview.superview; 

      for (UIView* vx in v1.subviews) { 
       Class dimmingViewClass = NSClassFromString(@"UIDimmingView"); 
       if ([vx isKindOfClass:[dimmingViewClass class]]) 
       { 
        UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(closePopoverOnSwipe)]; 
        [vx addGestureRecognizer:pan]; 
       } 
      } 
}]; 

你必須持有點擊手勢,將關閉UIDimmingView。只需添加到它。我正在使用Class dimmingViewClass = NSClassFromString(@"UIDimmingView");以避免直接使用未記錄的API。我還沒有嘗試將這個黑客發送給蘋果,但下週將嘗試。我希望它會通過。但我測試了這個,它確實打電話給我的選擇器。