2012-10-11 76 views
2

我想知道如果有人知道如何實現「觸摸裏面」的反應,當用戶按下然後舉起他們的手指touchesBegan,touchesEnded方法。我知道這可以通過UITapGestureRecognizer來完成,但實際上我正在努力使其僅適用於快速點擊(使用UITapGestureRecognizer,如果您長時間握住手指,然後擡起,它仍會執行)。任何人都知道如何實現這一點?如何實現在觸摸觸摸內部touckingBegin,touchesEnded

回答

4

使用UILongPressGesturizer實際上是一個更好的解決方案,以模仿所有的UIButtontouchUpInsidetouchUpOutsidetouchDown等)的功能:

- (void) longPress:(UILongPressGestureRecognizer *)longPressGestureRecognizer 
{ 
    if (longPressGestureRecognizer.state == UIGestureRecognizerStateBegan || longPressGestureRecognizer.state == UIGestureRecognizerStateChanged) 
    { 

     CGPoint touchedPoint = [longPressGestureRecognizer locationInView: self]; 

     if (CGRectContainsPoint(self.bounds, touchedPoint)) 
     { 
      [self addHighlights]; 
     } 
     else 
     { 
      [self removeHighlights]; 
     } 
    } 
    else if (longPressGestureRecognizer.state == UIGestureRecognizerStateEnded) 
    { 
     if (self.highlightView.superview) 
     { 
      [self removeHighlights]; 
     } 

     CGPoint touchedPoint = [longPressGestureRecognizer locationInView: self]; 

     if (CGRectContainsPoint(self.bounds, touchedPoint)) 
     { 
      if ([self.delegate respondsToSelector:@selector(buttonViewDidTouchUpInside:)]) 
      { 
       [self.delegate buttonViewDidTouchUpInside:self]; 
      } 
     } 
    } 
} 
2

您可以通過創建一個UIView子類並在其中實現來實現touchesBegan和touchesEnded。

但是,您也可以使用UILongPressGestureRecognizer並獲得相同的結果。

0

你可以創建一些BOOL變量然後在-touchesBegan檢查什麼樣的視圖或任何你需要被觸摸,並將此BOOL變量設置爲YES。之後,在-touchesEnded檢查這個變量是否爲YES,並且您的視圖或任何您需要的內容被觸及,這將是您的-touchUpInside。之後當然設置BOOL變量爲NO

0

您可以添加一個UTapGestureRecognizer和一個UILongPressGestureRecognizer,並使用[tap requiresGestureRecognizerToFail:longPress];(點擊並長按是添加的識別器的對象)添加依賴項。

這樣,如果長時間按下,水龍頭將不會被檢測到。

2

我通過在touchesBegan中觸發一個計時器來做到這一點。如果這個計時器在touchesEnded被調用時仍在運行,那麼執行你想要的任何代碼。這給了touchUpInside的效果。

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     NSTimer *tapTimer = [[NSTimer scheduledTimerWithTimeInterval:.15 invocation:nil repeats:NO] retain]; 
     self.tapTimer = tapTimer; 
     [tapTimer release]; 
    } 

    -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     if ([self.tapTimer isValid]) 
     { 

     } 
    } 
+0

這是很聰明的,易於理解。 –