1. 程式人生 > >UIAlertView UITapGestureRecognizer 點選視窗之外區域關閉

UIAlertView UITapGestureRecognizer 點選視窗之外區域關閉

- (IBAction)showAlert:(id)sender {
  alert = [[UIAlertView alloc] initWithTitle:@"模態測試"
                                     message:@"請點選四周的模態區域我就消失"
                                    delegate:nil
                           cancelButtonTitle:@"確定"
                           otherButtonTitles:nil];
  [alert show];
  recognizerTap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                          action:@selector(handleTapBehind:)];
  
  [recognizerTap setNumberOfTapsRequired:1];
  recognizerTap.cancelsTouchesInView = NO; 
  [alert.window addGestureRecognizer:recognizerTap];
}
- (void)handleTapBehind:(UITapGestureRecognizer *)sender{
  if (sender.state == UIGestureRecognizerStateEnded){
    CGPoint location = [sender locationInView:nil];
    if (![alert pointInside:[alert convertPoint:location fromView:alert.window] withEvent:nil]){
      [alert.window removeGestureRecognizer:sender];
      [alert dismissWithClickedButtonIndex:0
                                  animated:YES];
    }
  }
}



彈出一個UIAlertView,然後點選視窗之外的區域來關閉UIALertView,上述程式碼在ios6.x及之前版本都有效,但是ios7之後這樣的處理就達不到這樣的效果了。。。

有木有人處理個這樣的問題?ios7該如何處理?折騰了半天都實現了不。

實在不行最後就得來個自己實現UIAlertView類似的功能了。。。。。。