2012-12-21 114 views
6

我有一些問題試圖實施三指捏。三指捏手勢

我一直在使用2個手指捏與2個手指旋轉,單獨! (不需要同時需要或需要的手勢)問題是很多時候,系統識別錯誤的移動,因爲它們都非常相似,所以我最終不得不移除手指並再次按下,以便嘗試使系統識別旋轉(通常它確定捏首先)

我搜查了很多,看看delayBegin會幫助,或者如果我可以做一些激活同時手勢,但沒有工作正常,所以我的想法是,而不是使用2個手指捏,我可以使用3(因爲捏比旋轉更容易)。

問題是,正如你所知,捏只能用2個手指工作。所以我決定我可以繼承UIPinchGestureReconizer,只允許它在屏幕上有3個手指時工作。其餘的它可以作爲標準夾點工作,甚至忽略三指(計算比例尺),但確保第三根手指仍在屏幕上。

所以,我想下面的實現爲我ThreeFingerPinchRecognizer(該子類UIPinchGestureRecognizer

@implementation GRThreeFingerPinchRecognizer 

-(id)initWithTarget:(id)target action:(SEL)action 
    { 
    self = [super initWithTarget:target action:action]; 
    if(self){ 
    } 
    return self; 
} 

- (void)reset 
{ 
    [super reset]; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    int numberOfTouches = event.allTouches.count; 
    if (numberOfTouches == 3) 
    { 
     [super touchesBegan:touches withEvent:event]; 
    } 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    int numberOfTouches = event.allTouches.count; 
    if (numberOfTouches == 3) 
    { 
     [super touchesMoved:touches withEvent:event]; 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
     [super touchesEnded:touches withEvent:event]; 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesCancelled:touches withEvent:event]; 
} 

所以,你可以看到,我試圖讓2手指捏的相同的功能(通過只有調用[super]功能,並在touchesBegantouchesMoved功能,我測試是否有在屏幕上3個手指(通過看event.alltouches.count

這樣,旋轉正在完善與兩個手指,但捏是n工作得很好,它很難激活它,當它的時候,它不工作,因爲兩個手指捏...

我知道我可能會做這完全錯誤,所以任何幫助將是偉大的!

非常感謝!

+0

野趣的方式,但PinchGestureRecognizer(這到底是要調用)可能期待觸碰2不3,我或許會嘗試刪除其中一個觸摸之前,你打電話super..you將不得不找出哪一個要刪除雖然.. – Daniel

+0

我相信這是在PinchGesture裏面處理,因爲我沒有改變事件傳遞的任何東西,我只轉發它!如果我刪除了「如果」,捏可以正常工作(但用2個手指) –

+0

*一個不錯的提示:3個手指功能在設置中啓用縮放功能的用戶設備上不起作用(Apple功能中可以雙擊3手指放大(在任何應用程序中,很像截圖功能在任何應用程序中的工作方式))...使用3更精細的應用程序檢測功能可能與使用家庭按鈕檢測功能一樣致命。 (我與許多其他人一起啓用了此縮放功能) –

回答

0

看到這個片段可以幫助ü確定捏的狀態:

if (pinch.numberOfTouches > 1) 
{ 
    CGPoint firstPoint = [pinch locationOfTouch:0 inView:self]; 
    CGPoint secPoint = [pinch locationOfTouch:1 inView:self]; 
    currentUpperY = MIN(firstPoint.y, secPoint.y); 
    if (previousY == 0) previousY = currentUpperY; 
    Float32 y = (self.contentOffset.y + previousY - currentUpperY); 
    [self setContentOffset:CGPointMake(0, y < 0 ? 0 : y) animated:NO]; 

    if (pinch.state == UIGestureRecognizerStateBegan) 
    { 
     pinchStarted = YES; 
     firstY = MIN(firstPoint.y, secPoint.y); 
     secondY = MAX(firstPoint.y, secPoint.y); 
     NSArray *pinchedIndexs = [self indexPathsForRowsInRect:CGRectMake(0.0, firstY, CGRectGetWidth(self.bounds), secondY)]; 
     if (pinchedIndexs.count) itemToOpenOrClose = [[currentItems subarrayWithRange:NSMakeRange(((NSIndexPath *)[pinchedIndexs objectAtIndex:0]).row, pinchedIndexs.count - 1)] copy]; 
    } 
} 

if ((pinch.state == UIGestureRecognizerStateChanged && pinchStarted && itemToOpenOrClose.count) 
    || pinch.state == UIGestureRecognizerStateEnded) 
{ 
    if (pinch.scale > 1) // Pinch OUT 
    { 
     for (Item *item in itemToOpenOrClose) 
     {     
      [self openItem:item inIndexPath:[NSIndexPath indexPathForRow:[currentItems indexOfObject:item] inSection:0]]; 
     } 
    } 
    else if (pinch.scale < 1) // Pinch IN 
    { 
     for (Item *item in itemToOpenOrClose) 
     { 
      [self closeItem:item inIndexPath:[NSIndexPath indexPathForRow:[currentItems indexOfObject:item] inSection:0]]; 
     } 
    } 

    if (pinch.state == UIGestureRecognizerStateEnded) 
    { 
     pinchStarted = NO; 
     itemToOpenOrClose = nil; 
     previousY = 0; 
    } 
}