0

所以我當前的項目有一個平移手勢識別器,如果我已經移動到屏幕頂部,它應該向上滾動以考慮該手勢。雖然手勢尚未結束,手勢的當前位置仍保留在屏幕的頂部,但我想要不斷地滾動。我的問題是手勢識別器只有在狀態發生變化時纔會被調用,因此只有當您在頂部來回移動時,我的內容纔會滾動,而不是在手勢繼續位於頂部時不停地滾動。有沒有合理的方式來持續調用代碼,而手勢還沒有結束,但不一定會改變?下面是我有什麼僞代碼:雖然泛姿態尚未結束,但定期事件

- (void)handleGestureRecognizer:(UIGestureRecognizer *)gesture { 
    if (gesture.state == UIGestureRecognizerStateChanged) { 
     CGPoint point = [gesture locationInView:self.view]; 
     if (point.y < 100) { 
      //I would like this code to be called continually while the 
      //gesture hasn't ended, not necessarily only when it changes 
      [self updateScrollPosition]; 
     } 
} 

我能想到的幾個貧民窟的方式通過基於識別的當前狀態設定狀態的bool和創造我自己的計時器定期檢查做到這一點,但它看起來很詭異,我不特別喜歡它,所以我想知道是否有人可以提出一個更清潔的解決方案。使用計時器和感覺更好

+0

沒有被肯定你可以試試..如果(gesture.state! = UIGestureRecognizerEnded && point.y <100)[self updateScrollPosition];或嘗試一個「時間」的方法.. – snksnk

+0

我正要建議計時器,然後我讀了「貧民窟」:-),但我認爲這不是貧民窟的意義,即sdk手勢使用定時器遍佈整個地方,只是隱藏應用程序。這可能有點冗長,但我認爲計時器是一個好方法。 – danh

回答

1

的一種方法是通過使用performSelector隱瞞了一點:withObject:afterDelay:

- (void)stillGesturing { 
    [self updateScrollPosition]; 
    [NSObject cancelPreviousPerformRequestsWithTarget:self]; 
    [self performSelector:@selector(stillGesturing) withObject:nil afterDelay:0.5]; 
} 

// then in the recognizer target 
if (gesture.state == UIGestureRecognizerStateEnded) { 
    [NSObject cancelPreviousPerformRequestsWithTarget:self]; 
} else if (gesture.state == UIGestureRecognizerStateChanged) { 
    CGPoint point = [gesture locationInView:self.view]; 
    if (point.y < 100) { 
     //I would like this code to be called continually while the 
     //gesture hasn't ended, not necessarily only when it changes 
     [self stillGesturing]; 
    } 
} 
+0

如果方法在手勢結束時執行,這不會導致競爭條件嗎?反正也有目標選擇器取消,在關閉的情況下,我有其他選擇器可能被稱爲? –

+0

沒有競爭條件,主隊列將被序列化(並且stillGesturing不會讓新消息在清除之前排隊),但是,這會不加區分地取消任何其他延期執行。 (一個適當的計時器會讓你有選擇性,我只是試圖隱藏貧民區) – danh

+0

要做適當的計時器,只需用schedule替換執行,並用invalidate替換cancelPrev。 (您需要將其保留在屬性中,以便在手勢結束時使其失效)。 – danh