您的位置:首页 > 其它

FormSheet式模态视图,点击模态视图外隐藏模态视图的方法

2015-07-16 12:44 387 查看
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

_tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleTapBehind:)];
[_tapRecognizer setNumberOfTapsRequired:1];
_tapRecognizer.cancelsTouchesInView = NO;
[self.view.window addGestureRecognizer:_tapRecognizer];
[_tapRecognizer setDelegate:(id<UIGestureRecognizerDelegate>)self];
}

- (void)handleTapBehind:(UITapGestureRecognizer *)sender
{

if (sender.state == UIGestureRecognizerStateEnded) {

CGPoint location = [sender locationInView:nil];

if (UIDevice currentDevice]systemVersion] floatValue] < 8.0) {

if (UIInterfaceOrientationIsLandscape([UIApplicationsharedApplication].statusBarOrientation)) {

location = CGPointMake(location.y, location.x);
}
}

if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil]) {

[self.view.window removeGestureRecognizer:sender];
[self dismissViewControllerAnimated:YES completion:nil];
}
}
}

#pragma mark - UIGestureRecognizer Delegate

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

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

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return YES;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: