UIScrollView指定区域不滑动
2018-05-12 本文已影响209人
LX2014
假如UIScrollView上有一个色盘colorView,在滑动色盘上的按钮和滑动色盘改变选中颜色时,底部的scrollView不滚动。
设置UIScrollView的属性:scrollView.delaysContentTouches = NO;
那么在自定义colorView中实现类似如下方法:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"TestView touchesBegan");
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
CGRect rect = self.iamgeView.frame;
if (CGRectContainsPoint(rect, point)) {
NSLog(@"touchesBegan=========");
if ([self.nextResponder isKindOfClass:[UIScrollView class]]) {
NSLog(@"touchesBega--------");
[(UIScrollView *)self.nextResponder setScrollEnabled:NO];
}
}
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if ([self.nextResponder isKindOfClass:[UIScrollView class]]) {
NSLog(@"touchesBega--------");
[(UIScrollView *)self.nextResponder setScrollEnabled:YES];
}
}
注意要设置:scrollView.delaysContentTouches = NO;否则colorView的点击事件不会执行。
另外如果同一个UIScrollView的部分子view需要快速响应事件,有的不用快速响应事件,那么需要用:
//自定义UIScrollView中
- (BOOL)touchesShouldBegin:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
return YES;
}
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
return YES;
}
如果需要在滑动时,点击在button上响应滑动事件,在UISlider上响应UISlider的滑动事件那么需要做如下设置:
//scrollView设置
scrollview.delaysContentTouches = NO;
//自定义scrollView中
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
return YES;
}