scrollview/tableview 無法響應touch事件
阿新 • • 發佈:2018-12-19
scrollview 及其子檢視無法響應touch事件
辦法一:給view新增點選事件
- (void)viewDidLoad { [super viewDidLoad]; UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)]; //設定成NO表示當前控制元件響應後會傳播到其他控制元件上,預設為YES。 tapGestureRecognizer.cancelsTouchesInView = NO; //將觸控事件新增到當前view [self.view addGestureRecognizer:tapGestureRecognizer]; } -(void)keyboardHide:(UITapGestureRecognizer*)tap{ [textFiled resignFirstResponder]; }
辦法二:建立scrollview的分類,重寫touch方法
@implementation UIScrollView (Touch) - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touch begin"); if(!self.dragging) { [[self nextResponder] touchesBegan:touches withEvent:event]; } [super touchesBegan:touches withEvent:event]; } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { if(!self.dragging) { [[self nextResponder] touchesMoved:touches withEvent:event]; } [super touchesMoved:touches withEvent:event]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if(!self.dragging) { [[self nextResponder] touchesEnded:touches withEvent:event]; } [super touchesEnded:touches withEvent:event]; } @end