@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.CSS);@import url(/css/cuteeditor.css);#PRagma -mark点击模态视图外的空白处隐藏模态视图
- (void)viewDidAppear:(BOOL)animated
{
[superviewDidAppear:animated];
_tapRecognizer= [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleTapBehind:)];
[_tapRecognizersetNumberOfTapsRequired:1];
_tapRecognizer.cancelsTouchesInView=NO;//So the user can still interact with controls in the modal view
[self.view.windowaddGestureRecognizer:_tapRecognizer];
[_tapRecognizersetDelegate:(id<UIGestureRecognizerDelegate>)self];
}
- (void)handleTapBehind:(UITapGestureRecognizer*)sender
{
if(sender.state==UIGestureRecognizerStateEnded) {
// passing nil gives us coordinates in the window
CGPointlocation = [senderlocationInView:nil];
// swap (x,y) on iOS 8 in landscape
if([[UIDevicecurrentDevice].systemVersionfloatValue] >=8.0f) {
if(UIInterfaceOrientationIsLandscape([UIapplicationsharedApplication].statusBarOrientation)) {
location =CGPointMake(location.y, location.x);
}
}
// convert the tap's location into the local view's coordinate system, and test to see if it's in or outside. If outside, dismiss the view.
if(![self.viewpointInside:[self.viewconvertPoint:locationfromView:self.view.window]withEvent:nil]) {
// remove the recognizer first so it's view.window is valid
[self.view.windowremoveGestureRecognizer:sender];
[selfdismissViewControllerAnimated:YEScompletion:nil];
}
}
}
#pragma mark - UIGestureRecognizer Delegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer
{
returnYES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer
{
returnYES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch
{
returnYES;
}
转载:http://my.oschina.net/CreeveLiu/blog/347913
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);