1
A
回答
3
是的,這是可能的。這非常簡單。
您可以保存觸摸開始時的當前時間(即[NSDate date]),並獲取觸摸結束的當前時間與保存的開始時間之間的差異。
@interface MyViewController : UIViewController {
NSDate *startDate;
}
@property (nonatomic, copy) NSDate *startDate;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.startDate = [NSDate date];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSTimeInterval ti = [[NSDate date] timeIntervalSinceDate:self.startDate];
NSLog(@"Time: %f", ti);
}
1
在你的頭
感謝,使一個NSDate屬性,像這樣:
@property(nonatomic, retain) NSDate *touchesBeganDate;
然後,在touchesBegan
方法,這樣做:
self.touchesBeganDate = [NSDate date];
最後,在touchEnd
方法中:
NSDate *touchesEndDate = [NSDate date];
NSTimeInterval touchDuration = [touchesEndDate timeIntervalSinceDate:
self.touchesBeganDate];
self.touchesBeganDate = nil;
NSTimeInterval可用作普通浮點型變量。
編碼愉快:)
噢,記得@synthesize
的touchesBeganDate
。
3
與上述內容略有不同的答案;使用UITouch對象上的timestamp property,而不是從NSDate獲取當前時間。它是一個NSTimeInterval(即一個C整型)而不是一個NSDate對象。所以,例如
// include an NSTimeInterval member variable in your class definition
@interface ...your class...
{
NSTimeInterval timeStampAtTouchesBegan;
}
// use it to store timestamp at touches began
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *interestingTouch = [touches anyObject]; // or whatever you do
timeStampAtTouchesBegan = interestingTouch.timestamp // assuming no getter/setter
}
// and use simple arithmetic at touches ended
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if([touches containsObject:theTouchYouAreTracking])
{
NSLog(@"That was a fun %0.2f seconds", theTouchYouAreTracking.timestamp - timeStampAtTouchesBegan);
}
}
相關問題
- 1. 在android中查找觸摸事件之間的時間間隔
- 2. 如何找到R中兩個事件之間的時間差?
- 3. SQL Server查詢 - 事件之間的事件時間總和
- 4. 在MySQL中查找最近的時間和之後的時間
- 5. SQL查詢在很長時間內查找現在和時間之間的行
- 6. 在iPhone中查找當前時間
- 7. 如何在Python 3中創建時間盒裝查找事件?
- 8. SQL Server在開始時間和結束時間之間查找時間片
- 9. 在iPhone之間發送事件
- 10. 如何計算兩個「keyup」事件之間的時間間隔
- 11. 查找列之間的時間差
- 12. 查找行之間的時間差
- 13. 在iPhone上缺少touchEnd事件
- 14. touchend事件不在iphone上發射
- 15. 如何計算R中兩個事件之間的時間差?
- 16. 如何比較Iphone和(網絡)服務器之間的時間?
- 17. ORACLE SQL - 事件之間的時間?
- 18. 2個事件之間的時間
- 19. 查找excel中兩個日期/時間之間的天數和時間
- 20. 如何查找2個即時時間戳之間的持續時間
- 21. Kenson-Maier曲線審查事件不會顯示審查事件和事件時間之間的聯繫
- 22. 如何查找熊貓時間序列的事件持續時間
- 23. 如何在php中查找日期時間類型和當前日期/時間之間的差異?
- 24. 如何查找時間之間的差異
- 25. 在期間/時間X和時間Y中查找對象Y
- 26. 如何在iPhone中查找時間格式模式?
- 27. 如何在javascript中查找兩次之間的時差
- 28. SQL查詢來查找日期和時間之間記錄
- 29. 查找和2個文件之間
- 30. 如何查找在java中的持續時間之間的區別?