我在我的视图控制器中有几个UIScrollView
s。我想覆盖通过UIPanGestureRecognizer
拍摄双指滑动的视图,该视图不会记录UIScrollView
滑动手势。只捕获UIView 2手指UIPanGestureRecognizer
当我用双指手势手势在透明视图上显示我的内容时,未检测到我的轻敲和1个手指轻扫。我试图覆盖pointInside:
方法返回NO
但它不记录我的2指滑动。
效果类似于用4根手指轻扫来改变应用程序。
我在我的视图控制器中有几个UIScrollView
s。我想覆盖通过UIPanGestureRecognizer
拍摄双指滑动的视图,该视图不会记录UIScrollView
滑动手势。只捕获UIView 2手指UIPanGestureRecognizer
当我用双指手势手势在透明视图上显示我的内容时,未检测到我的轻敲和1个手指轻扫。我试图覆盖pointInside:
方法返回NO
但它不记录我的2指滑动。
效果类似于用4根手指轻扫来改变应用程序。
您不需要覆盖视图。
首先实现UIPanGestureRecognizer
将处理2指平移,并将其分配到包含UIScrollView
小号
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)];
panGestureRecognizer.delegate = self;
panGestureRecognizer.minimumNumberOfTouches = 2;
panGestureRecognizer.maximumNumberOfTouches = 2;
[self.view addGestureRecognizer:panGestureRecognizer];
使用UIGestureRecognizerDelegate
处理2指平移与UIScrollView
平移手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
最后,你有你的观点能够处理2个手指锅
- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer
{
NSLog(@"pan");
}
如果你想停止时,检测到两个手指平移您可以禁用和启用UIScrollView
泛识别
- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer
{
if(gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
_scrollView.panGestureRecognizer.enabled = NO;
}
if(gestureRecognizer.state == UIGestureRecognizerStateEnded)
{
_scrollView.panGestureRecognizer.enabled = YES;
}
NSLog(@"pan");
}
如果你不需要覆盖层,你可以用手势识别器来解决这个问题。我写这件事作为一个测试:
- (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手指手势识别器。希望有所帮助!
所以希望你scrollviews没有捕捉到2个手指挥笔滚动
UIScrollView
? – peko 2013-04-30 15:08:38多数民众赞成1部分我猜 – 2013-04-30 16:36:08
@AndyJacobs你想滚动视图来检测泛或不?你想添加躺在这将检测2手指锅和滚动视图来检测其他锅右?如果我想错了,请澄清一下 – 2013-05-09 10:54:05