天天看點

關于UIScrollView不能響應UITouch事件的解決辦法

這個不用多說直接上代碼。

原因是:UIView的touch事件被UIScrollView捕獲了。

解決辦法:讓UIScrollView将事件傳遞過去。于是最簡單的解決辦法就是加一個UIScrollView的category。這樣每個用到UIScrollView的地方隻要導入這個category就可以直接響應相關的touch事件了。

類似問題:在論壇看見很多人說UIImageView也沒辦法響應,我沒嘗試過,不過我想解決辦法和原因都是一樣的。 如果有人嘗試過,不一樣,歡迎批評指正。

#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