本人在控制器重寫了3個方法外驱,touchesBegan,touchesMoved 和touchesEnded氓癌,控制器中有一個 scrollView番舆,最后的結(jié)果是那3個方法都不走,原因是被scrollView攔截了捍歪。
有一種辦法是設置scrollView.userInteractionEnabled = NO,關(guān)閉交互就會走那3個方法户辱,但是這種方法不太好,因為scrollView自身一般都會有其它交互糙臼。
在網(wǎng)上查了一些方法庐镐,大概有2種:
第一種:自定義一個scrollView,然后在自定義的scrollView中弓摘,重寫那3個方法
第二種:寫一個 scrollView的分類焚鹊,如UIScrollView+Touch ,在分類中重寫那3個方法痕届,如:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesEnded:touches withEvent:event];
}
網(wǎng)上的這種做法管不管用韧献?應該說管用一部分,因為網(wǎng)上緊緊是針對touchesBegan這一個方法而言研叫,只要在子類或者分類中重寫了那3個方法锤窑,控制器的touchesBegan就會觸發(fā)。
但是嚷炉,我要的是控制器的那3個方法都要走渊啰,而且走的流暢。現(xiàn)在按照網(wǎng)上的這種做法,現(xiàn)象是绘证,touchesBegan和touchesEnded會走隧膏,然而touchesMoved走得不流暢,不管你的手指移動多長的距離嚷那,touchesMoved通常只走一次胞枕,偶爾2次,這是為什么魏宽?
我們來分析一下腐泻,子類和分類中重寫的那3個方法中,[self nextResponder]指的是誰队询?指的應該是控制器的view派桩,而不是控制器,控制器的view的下一個響應者才是控制器蚌斩,所以不應該寫[self nextResponder],應該寫[[self nextResponder] nextResponder]铆惑,如:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[[self nextResponder] nextResponder] touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[[[self nextResponder] nextResponder] touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[[[self nextResponder] nextResponder] touchesEnded:touches withEvent:event];
}
或者在分類中重寫那3個方法,然后super一下也是沒問題的,注意是分類送膳,不是子類
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
}