仅捕获 UIView 2 手指 UIPanGestureRecognizer
Posted
技术标签:
【中文标题】仅捕获 UIView 2 手指 UIPanGestureRecognizer【英文标题】:Capture only UIView 2 finger UIPanGestureRecognizer 【发布时间】:2013-04-30 13:36:42 【问题描述】:我的视图控制器中有几个UIScrollView
s。我想覆盖一个通过UIPanGestureRecognizer
捕获两指滑动的视图,它不会记录UIScrollView
滑动手势。
当我使用 2 指平移手势在内容上放置透明视图时,未检测到我的点击和 1 指滑动。我尝试覆盖pointInside:
方法以返回NO
但它不会记录我的两指滑动。
效果类似于四指滑动切换应用。
【问题讨论】:
所以希望您的滚动视图不捕获 2 次手指滑动? @AndyJacobs 你想让滚动视图检测到平移吗?您想添加覆盖层,它将检测 2 个手指平移和滚动视图以检测其他平移,对吗?如果我认为它错了,请澄清它 【参考方案1】:您不需要叠加视图。
首先实现UIPanGestureRecognizer
,它将处理2个手指平移并将其分配给包含UIScrollView
s的视图
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)];
panGestureRecognizer.delegate = self;
panGestureRecognizer.minimumNumberOfTouches = 2;
panGestureRecognizer.maximumNumberOfTouches = 2;
[self.view addGestureRecognizer:panGestureRecognizer];
使用UIGestureRecognizerDelegate
以UIScrollView
平移手势处理2 个手指平移
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
return YES;
最后你可以处理 2 个手指平移
- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer
NSLog(@"pan");
如果您想在检测到两个手指平移时停止滚动UIScrollView
,您可以禁用和启用UIScrollView
平移识别器
- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer
if(gestureRecognizer.state == UIGestureRecognizerStateBegan)
_scrollView.panGestureRecognizer.enabled = NO;
if(gestureRecognizer.state == UIGestureRecognizerStateEnded)
_scrollView.panGestureRecognizer.enabled = YES;
NSLog(@"pan");
【讨论】:
【参考方案2】:如果您真的不需要覆盖,您可以只使用手势识别器来解决这个问题。我写了这个作为测试:
- (void)viewDidLoad
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
_scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, self.view.bounds.size.height);
UIView *green = [[UIView alloc] initWithFrame:self.view.bounds];
[green setBackgroundColor:[UIColor greenColor]];
UIView *blue = [[UIView alloc] initWithFrame:CGRectOffset(self.view.bounds, self.view.bounds.size.width, 0)];
[blue setBackgroundColor:[UIColor blueColor]];
[_scrollView addSubview:green];
[_scrollView addSubview:blue];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPan:)];
[pan setMinimumNumberOfTouches:2];
[pan setMaximumNumberOfTouches:2];
[pan setDelaysTouchesBegan:YES];
[_scrollView addGestureRecognizer:pan];
[self.view addSubview:_scrollView];
- (void)twoFingerPan:(UIPanGestureRecognizer *)gesture
switch (gesture.state)
case UIGestureRecognizerStateBegan:
self.scrollView.scrollEnabled = NO;
break;
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateFailed:
self.scrollView.scrollEnabled = YES;
break;
default:
break;
NSLog(@"2 Fingers!");
我收到twoFingerPan:
回电,要求使用 2 根手指。滚动视图的panGestureRecognizer
那时仍在工作,因此我禁用滚动视图上的滚动以处理 2 指平移。我发现这种方法效果很好。一种奇怪的事情是,如果滚动视图正在减速,则不会调用 2 指手势识别器。希望对您有所帮助!
【讨论】:
以上是关于仅捕获 UIView 2 手指 UIPanGestureRecognizer的主要内容,如果未能解决你的问题,请参考以下文章