有時候會遇到兩個scrollview在一起嵌套使用,滑動沖突是必須要解決的
如:在一個scrollview上面放置另一個scrollview 通過監聽手指點擊的位置來判斷點擊的是哪一個scrollview來進行處理
首先自定義一個scrollview在里面重載以下方法
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
// 獲取一個UITouch
UITouch *touch = [touches anyObject];
// 獲取當前的位置
CGPoint current = [touch locationInView:self];
CGFloat y = 280;
if (current.y >= y+ 10) {
//在地圖上
NSLog(@"滾動地圖");
return YES;
} else {
return [super touchesShouldBegin:touches withEvent:event inContentView:view];
}
}
touchesShouldCancelInContentView從字面理解大概意思為 應該取消當前視圖的觸摸事件
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
if ([view isKindOfClass:NSClassFromString(@"TapDetectingView")]) {
//在地圖上返回NO
return NO;
} else {
return [super touchesShouldCancelInContentView:view];
}
}
self.delaysContentTouches = NO;
當手指放到UIScrollview上時,uiscrollview會等待一個時間段才去響應觸摸事件,這個屬性關閉后無論手指移動的多么快都可以直接響應事件