我在我的視圖控制器中有幾個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