UIAlertView点击背景消失的方法

来源:互联网 发布:针织女套裙淘宝网 编辑:程序博客网 时间:2024/05/25 18:12
_phoneAlert = [[UIAlertView alloc]initWithTitle:@"" message:@"请选择要选择的电话" delegate:self cancelButtonTitle:nil otherButtonTitles:@"商家电话",@"客服电话",@"客服微信", nil];    [_phoneAlert show];        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];    tap.numberOfTapsRequired = 1;    tap.cancelsTouchesInView = NO;    [[UIApplication sharedApplication].keyWindow addGestureRecognizer:tap];

- (void)tap:(UITapGestureRecognizer *)tap{    if (tap.state == UIGestureRecognizerStateEnded){        CGPoint location = [tap locationInView:nil];        if (![_phoneAlert pointInside:[_phoneAlert convertPoint:location fromView:_phoneAlert.window] withEvent:nil]){            [_phoneAlert.window removeGestureRecognizer:tap];            [_phoneAlert dismissWithClickedButtonIndex:0 animated:YES];        }    }}

注意AlerView是在window上的,不是在view上的。

0 0