iOS scrollView与touchesBegan等冲突的解决办法

本人在控制器重写了3个方法,touchesBegan,touchesMoved 和touchesEnded,控制器中有一个 scrollView,最后的结果是那3个方法都不走,原因是被scrollView拦截了。
有一种办法是设置scrollView.userInteractionEnabled = NO,关闭交互就会走那3个方法,但是这种方法不太好,因为scrollView自身一般都会有其它交互。
在网上查了一些方法,大概有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];
}

网上的这种做法管不管用?应该说管用一部分,因为网上紧紧是针对touchesBegan这一个方法而言,只要在子类或者分类中重写了那3个方法,控制器的touchesBegan就会触发。
但是,我要的是控制器的那3个方法都要走,而且走的流畅。现在按照网上的这种做法,现象是,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];
}

你可能感兴趣的:(iOS scrollView与touchesBegan等冲突的解决办法)