2011-07-25 33 views
0

我想向我的應用程序添加特殊手勢。適用於iOS的特殊手勢

例如,如果用戶在屏幕上滑動X,我想將其視爲刪除,或者如果用戶將滑動V - 我想將其視爲確認。

我在考慮對UIGesture類之一進行子類化,但不知道如何檢測我需要的東西。

更新:我找到了複選標記手勢的示例(http://conceitedcode.com/2010/09/custom-gesture-recognizers/),但沒有線索如何實施X之一。

+0

iOS可能還不支持隨機手勢,如繪圖。大多數可用的手勢都是明確的滑動,點擊和轉動。 iOS 5似乎有這樣的稱爲輔助觸摸的東西,這可能表示稍後能夠重載自定義繪圖式手勢。 –

回答

3

你真的不認識一個「X」,那將是2個手勢。你將不得不做一些手勢保護,看看前一個是否是一個對角線筆劃,如果這個是一個...和各種瘋狂。你可以做對角線向下,直線向上,然後斜向另一個方向。生病讓你的代碼:P

但是,你想要做的是子類UIGestureRecognizer。 Here is some documentation on it。您需要實施以下方法:

- (void)reset; 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; 

這是用於識別「V」手勢的代碼。

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

    if ([self state] == UIGestureRecognizerStateFailed) 
     return; 

    CGPoint curr = [[touches anyObject] locationInView:self.view]; 
    CGPoint prev = [[touches anyObject] previousLocationInView:self.view]; 

    if (!strokeUp) { 
     // upstroke has increasing x value but decreasing y value 
     if (curr.x >= prev.x && curr.y <= prev.y) { 
      strokeUp = YES; 
     } else { 
      [self state] = UIGestureRecognizerStateFailed; 
     } 
    } 
} 

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

    if (([self state] == UIGestureRecognizerStatePossible) && strokeUp) { 
     [self state] = UIGestureRecognizerStateRecognized; 
    } 

} 

這應該讓你指出正確的方向,祝你好運。

+0

你實際上給了我一個關於如何解決它的想法:實現一個「對角」手勢,然後檢查其中的兩個 - 彼此非常接近(例如,小於1-2秒)。然後我會根據需要採取行動。 –

+0

祝你好運,玩得開心:P – ColdLogic