UIScrollView不能響應(yīng)UITouch事件的解決辦法
這個(gè)不用多說(shuō)直接上代碼。
原因是:UIView的touch事件被UIScrollView捕獲了。
解決辦法:讓UIScrollView將事件傳遞過(guò)去。于是最簡(jiǎn)單的解決辦法就是加一個(gè)UIScrollView的category。這樣每個(gè)用到UIScrollView的地方只要導(dǎo)入這個(gè)category就可以直接響應(yīng)相關(guān)的touch事件了。
類(lèi)似問(wèn)題:在論壇看見(jiàn)很多人說(shuō)UIImageView也沒(méi)辦法響應(yīng),我沒(méi)嘗試過(guò),不過(guò)我想解決辦法和原因都是一樣的。 如果有人嘗試過(guò),不一樣,歡迎批評(píng)指正。
- #import "UIScrollView+UITouch.h"
- @implementation UIScrollView (UITouch)
- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
- [[self nextResponder] touchesBegan:touches withEvent:event];
- [super touchesBegan:touches withEvent:event];
- }
- -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
- [[self nextResponder] touchesMoved:touches withEvent:event];
- [super touchesMoved:touches withEvent:event];
- }
- - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
- [[self nextResponder] touchesEnded:touches withEvent:event];
- [super touchesEnded:touches withEvent:event];
- }
- @end