2014-09-02 75 views
8

我們目前使用的解決方案類似於here(參見Ares的回答)。這似乎不適用於iOS8。iOS8:關閉外接水龍頭的表單

我有一個表單,我想在用戶點擊表單背後的暗視圖時立即關閉它。 以前,通過在窗口中添加手勢識別器並檢查分支位置以查看它是否位於當前表單之外,這似乎是可能的;

我也注意到該設備的點需要轉換(開關x和y)在橫向模式下使用。除此之外,現在它只接收表單內部發生的手勢,在屏幕上的任何點擊手勢之前觸發事件。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)]; 
    self.recognizer.numberOfTapsRequired = 1; 
    self.recognizer.cancelsTouchesInView = NO; 
    [self.view.window addGestureRecognizer:self.recognizer]; 
} 

- (void)handleTapBehind:(UITapGestureRecognizer *)sender 
{ 
    if (sender.state == UIGestureRecognizerStateEnded) 
    { 
     CGPoint location = [sender locationInView:nil]; 
     if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) && IOS8) 
     { 
      location = CGPointMake(location.y, location.x); 
     } 

     // if tap outside pincode inputscreen 
     BOOL inView = [self.view pointInside:[self.view convertPoint:location  fromView:self.view.window] withEvent:nil]; 
     if (!inView) 
     { 
      [self.view.window removeGestureRecognizer:sender]; 
      [self dismissViewControllerAnimated:YES completion:nil]; 
     } 
    } 
} 

回答

10

正如你所引用的線程所提到的,你應該添加一個UIGestureRecognizerDelegate和實施方法:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { 
    return YES; 
} 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 
    return YES; 
} 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { 
    return YES; 
} 
+0

我不知道你是什麼意思?這是如何反映ios8中的API更改?提到的代碼在ios7中工作正常。或者這是一種規避ios8錯誤的方法? – 2014-09-15 19:00:42

+0

爲了讓你發佈的代碼能夠在iOS8中工作,在你的'self.recognizer'中添加一個'UIGestureRecognizerDelegate'並添加這些代理方法 – Erich 2014-09-15 19:03:33

+0

是的,解決了它!謝謝:) – 2014-09-16 08:20:38