重写touchBegin 方法是不行的,在UITableView/UIScrollView
解决方案 重写hitTest:withEvent: 在他们的子类中
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
static UIEvent *e = nil;
if (e != nil && e == event) {
e = nil;
return [super hitTest:point withEvent:event];
}
e = event;
if (event.type == UIEventTypeTouches) {
NSSet *touches = [event touchesForView:self];
UITouch *touch = [touches anyObject];
if (touch.phase == UITouchPhaseBegan) {
NSLog(@"Touches began");
}else if(touch.phase == UITouchPhaseEnded){
NSLog(@"Touches Ended");
}else if(touch.phase == UITouchPhaseCancelled){
NSLog(@"Touches Cancelled");
}else if (touch.phase == UITouchPhaseMoved){
NSLog(@"Touches Moved");
}
}
return [super hitTest:point withEvent:event];
}
关于hitTest:withEvent:
字面意思是撞击测试,当手指触摸到当前屏幕上活跃的 app 界面。ios 系统会将 当前触摸操作 打包,具体就是UIEvent
屏幕上的每一次动作都是一个UITouch,多个UITouch 组成一次UIEvent. UIEvent 表示一次事件。
传递给当前活跃的App keyWindow.正常情况下 hitTest 确定屏幕上众多View 中哪一发生了事件。
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
返回值 不为nil 则一值 查找view 的子view 重复调用hisTest 确定事件。
这边还有别人给的一些方法:
重写touch方法http://blog.csdn.net/aaidong/article/details/45914435
添加手势识别器:http://my.oschina.net/u/1861789/blog/346622