实现view手势点击响应

在UIViewController中,加入协议UIGestureRecognizerDelegate,然后.m文件里加入以下代码:

UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

[self.view addGestureRecognizer:singleTap];

 //这个可以加到任何控件上,比如你只想响应WebView,我正好填满整个屏幕

singleTap.delegate = self;

singleTap.cancelsTouchesInView = NO;

[singleTap release];

然后有一个关键的,要实现一个方法:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{    

      return YES;

}

最后,响应的方法中,可以获取点击的坐标哦!

-(void)handleSingleTap:(UITapGestureRecognizer *)sender

{   

     CGPoint point = [sender locationInView:self.view]; 

     NSLog(@"handleSingleTap!pointx:%f,y:%f",point.x,point.y);

}

ShevaKuilin
388 声望32 粉丝

世间好物不坚牢,彩云易碎琉璃脆。