使用场景
如果您的app中使用了某一个tab页面来展示地图,就需要考虑对滑动手势进行区分,以便既能支持滑动地图,又能支持切换tab。
该demo给出了示例:快速滑动、从侧边开始滑动,则切换tab;否则滑动地图。
详细效果请见右侧视频。
用到产品
核心类/接口
类接口说明版本
UIPanGestureRecognizer----系统手势----
核心难点
实现手势处理
- (void)panHandler:(UIPanGestureRecognizer *)gesture
{
// NSLog(@"vvv %f, %f", [gesture velocityInView:_scrollView].x, [gesture velocityInView:_scrollView].y);
// 设置触发条件
if (gesture.state != UIGestureRecognizerStateEnded)
{
return;
}
if (fabs([gesture velocityInView:_scrollView].x) > 1200)
{
// -1 向左,1 向右
int direction = fabs([gesture velocityInView:_scrollView].x) / [gesture velocityInView:_scrollView].x;
int validIdx = 1;
if (_currentIndex == validIdx && !_scrollView.isDecelerating)
{
CGFloat width = _scrollView.frame.size.width * (1 - direction);
[_scrollView setContentOffset:CGPointMake(width, 0) animated:YES];
}
}
}
设置允许和scrollview上pan手势并发
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}