2011-09-29 234 views
0

我有一個名爲touchStatus的變量,用於跟蹤程序中的觸摸狀態。 touchesBegan方法中的變量設置爲B,touchesEnded中的變量設置爲E,touchesMoved中的變量設置爲M延遲觸摸響應

但是,我的要求有點不同。我被要求以某種方式進行編程,以便手指離開屏幕和touchStatus設置爲E之間存在一秒的延遲。如果用戶在一秒鐘之前觸摸屏幕,則touchStatus應繼續爲MB(無論是在一秒之前)。

我該如何做到這一點?

回答

1

您可以使用

[self performSelector:@selector(setEndedValue:) withObject:self afterDelay:1.0]; 

創建一個BOOL監視是否值應如設置:

BOOL hasTouchRestarted = NO; 

如果設置值前的畫面再次感動,將值更改爲YES並從setEndedValue方法返回。

-(void)setEndedValue { 
    if (hasTouchRestarted) { return; } 
    // set value 
    self.touchStatus = E; 
} 
+0

啊!非常感謝。那太簡單了。就像下面的一些人建議的那樣,我試圖使用定時器,但那是太多的代碼。無論如何,'performSelector:withObject:afterDelay'就像一個魅力。我以前應該想到這個! – Ravi

+0

當然可以!定時器非常脆弱,如果你沒有正確地使它們失效,你可以同時運行多個定時器。 PerformSelector爲你處理所有這些。 – seejaneworkit

0

在touchEnded例程中,設置NSTimer任務來在一秒鐘內調用選擇器。如果在此之前還有其他觸摸,請取消定時器任務。

0

使用NSTimer *計時器伊娃發起延遲呼叫,如果用戶在一秒鐘之前提起手指,則取消呼叫。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    self.myvar = @"B"; 
    [self.timer invalidate]; 
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleOneSecondPress) userInfo:nil repeats:NO]; 
} 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event { 
    [self.timer invalidate]; 
    self.timer = nil; 
} 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    self.myvar = @"M"; 
} 
- (void)handleOneSecondPress { 
    self.timer = nil; 
    self.myvar = @"E"; 
}